-
Notifications
You must be signed in to change notification settings - Fork 455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support jest config globalSetup and globalTeardown #411
Comments
There's an issue opened to do that on jest repository by using transform (see here). And if I correctly understand Jest behavior, ts-jest cannot manage this issue thanks to Jest architecture. Is there some ts-jest mainteners who can confirm that ? Update : I take a look to Jest source code, and the problem seems to come from this line. |
@yss14 this looks like an issue whose solution lies outside ts-jest (as @AamuLumi pointed out) All ts-jest (or any transformer) does is take in code and return the transpiled code. Jest would need to use the transformer when doing global setup and teardown to support this use case. I'm closing this issue but if I'm wrong and there's something we need to do here, it can be reopened again. |
Instead of waiting a fix, you can simulate globalSetup and globalTeardown by this way :
Output :
I hope this gonna help you ! :) |
@AamuLumi Thanks man! This is really what I'm looking for :) Will try this on monday! ;) |
@AamuLumi Which version do you use? For me |
Normally, typescript verification will not find jest, because programmatic use is not defined in types. But it should works at execution. Do you have installed jest locally instead of globally ? You can take a look at source code for explanation. |
@AamuLumi Can we set global variables that will be available in the tests with the workaround you have provided? |
I don't know. I saw tests are launched in vm for Node.js testing, so I'm not sure global variables are shared between startup code and test code. You can give it a try and tell us if that works or not. :) |
If anyone using typescript stumbles upon all of this with a naive question how would you hook it all up and invoke your typescript code out of this jest handler, I've described the exact sequence of steps in jestjs/jest#5164 (comment). |
why is this closed, has this been solved? |
FYI, the upstream issue with Jest is here: jestjs/jest#5164 |
I spent a few hours struggling with the same issue. Hopefully it can save some time for someone else. // tests/conversation.ts
export const conversationModule = function () {
test('create a conversation', async (done) => {
const user1: User = await AuthenticationService.registerUser('user1', 'test1', 'user1@gmail.com');
const user2: User = await AuthenticationService.registerUser('user2', 'test2', 'user2@gmail.com');
const conversation: Conversation = await ConversationService.createConversation(user1.id, user2.id);
expect(conversation.id).not.toBeNull();
done();
});
// ...
};
// tests/test-runner.ts
import { conversationModule } from './conversation';
import { authenticationModule } from './authentication';
beforeAll(async done => {
const connection = await createConnection('testing');
User.useConnection(connection);
Conversation.useConnection(connection);
Message.useConnection(connection);
done();
});
describe('Conversation module', conversationModule);
describe('Authentication module', authenticationModule);
afterAll(async (done: Function) => {
const connection = getConnection('testing');
await connection.close();
done();
}); |
With Jest 24, this is finally supported ;) |
@yss14 Can you show usage example? I'm trying to setup: jest.config.js:
jest.setup.ts:
*.spec.ts:
shows me 'undefined' |
@viT-1 See the jest docs.
|
@phikes hmm then how are we able to get those variables inside test suites? even typeorm getConnection don't work inside testsuites but works fine on teardown :( |
@hugo-dlb but if we have multiple tests we need to write all these for all tests file . We can create function but still we need to write beforeAll and afterAll. And the worst thing is with this way we need to open and close database connection everytime which has performance cost .Hmm what might be the best solution :(? |
@shirshak55 I did just that in our test suite. Just use |
@phikes but i have like 20 test suites :( and doing same repetition is really sad and I am frequently stopping and starting connection which makes tests too slow :( |
Does setupFileAfterEnv help ? |
OT, but I propose you look into how to run these in parallel, possibly each in a transaction and set your connection pool high enough. |
Figured this out today, just write your setup and teardown modules in Javascript and then import your Typescript modules, they will be transpiled on the fly. Example: sequelize.ts import { Sequelize } from 'sequelize';
export default new Sequelize(process.DATABASE_URL); setup.js require('dotenv').config();
const sequelize = require('./sequelize');
// remember importing your models so sequelize know about them.
const models = require('./models');
module.export = async () => {
await sequelize.sync();
}; |
This works, thanks so much. |
Hmm. I'm not sure why this worked for you and not for me, @renatoalencar. I'm doing pretty much the same thing, but rather than transpiling the require'd modules on the fly,
|
Thanks to @Ericnr's comment in jestjs/jest#5164, it seems the fix for me was to add this as the first line in require('ts-node').register({ transpileOnly: true }); Update: the line above fixed the compilation error, but for some reason the test code was still acting as if the global setup and teardown had not executed. So I kept looking and found @ecklf's suggestion on #1391. Using This StackOverflow question may also be interesting to others here: https://stackoverflow.com/questions/62470047/typeorm-connection-default-was-not-found-when-connection-is-created-in-jest-gl. If the setup/teardown of the database creates significant performance problems, a solution mentioned in the StackOverflow page is to create a test file that imports all the other tests, establishes the DB connection once and tears it down once. That's basically the approach @hugo-dlb mentioned here previously. |
That's probably because of the new major release. Glad you found another solution. |
Adding my two cents. I used
|
Amazingly, this works for me-- but only for globalSetup, not for globalTeardown. More crappy errors on the latter.
|
For globalSetup only, registering ts-node helped. For some reason, if I add the ts-node only in globalTeardown - both files work. |
can this be reopened? still no native support |
This issue is not in the scope of this repo. This repo is only performing code transpilation. If jest doesn’t ask ts-jest to transform codes, you will run into issues. You need to check your Jest config, and also you can ask in Jest Discord for help. |
Are there any plans to support the new jest feature
globalSetup
andglobalTeardown
?Those two configuration options are documented here and the PR for the new jest feature here.
Currently an error is thrown by jest, because the
.ts
files are not transpiled (I guess).My currently installed packages are
The text was updated successfully, but these errors were encountered: