How to prevent Jest from running tests as globalSetup is still running? - jestjs

My global setup code creates a dynamodb table and the global teardown destroys it. The code is just generic dynamodb.createTable() and dynamodb.deleteTable()
My basic test that just wants to return a list of inserted entities:
describe('Test suite', () => {
beforeAll(async () => {
await InsertEntity()
})
test('test', async () => {
const response = await getEntities()
expect(response.success).toBe(true)
})
})
My InsertEntities code:
export const InsertEntities = async () => {
const entity = new Entity({
// some data
})
const result = await entity.save()
console.log('entity', result)
}
But as soon as I run jest, it starts running the tests before the table is up, so it fails horribly. What can I do to stop this behavior?
What have I already tried: Running jest with --runInBand, adding jest.setTimeout() with various different timings going as up as 20secs. Changing the beforeAll to beforeEach. Checking how much time the table needs to go up: 10secs.
I've changed this code to run as setupFilesAfterEnv, but it still crashed. I've written similar code before and it still works! But not this time.
So, what am I missing from jest flow?

Related

Jest run code before and after all of the tests

I would like to run code before and after all the tests, not file wide, test wide. For example;
Before starting the e2e tests, I would like to run the server in test mode with the database. After all, I would like to flush my db and close the processes.
I don't know if it is possible but I also would like to have a global db variable to do tests. What I am currently doing is like this:
describe("Posts Module", () => {
let dbService: DatabaseService;
beforeAll(async () => {
dbService = new DatabaseService();
await dbService.init();
});
it("should give the posts", () => {
supertest(app)
.get("/posts")
.expect(200)
.then(async (response) => {
const dbPosts = await dbService.getPosts();
expect(response.body).toBeDefined();
expect(response.body.posts).toEqual(dbPosts);
});
});
afterAll(async () => {
await flushDb(dbService);
await dbService.close();
});
});
But what I actually want is initing this database service only once before all of the module tests (also starting the server, currently I start the server manually and run the tests afterwards).

Best way to setup files for jest and mongodb

Hihi.
I'm having a problem where jest it's sometimes failing a couple of tests randomly, most of the time because of this error "mongodb memory server cannot perform operation: a background operation is currently running for collection".
In another post I read something about building differents mongo instance for each block of tests.
What I have so far is a globalsetup file where I start the mongo replica set like this:
// global.ts
import { MongoMemoryReplSet } from "mongodb-memory-server";
const replSet = new MongoMemoryReplSet({
replSet: { storageEngine: "wiredTiger" },
});
module.exports = async () => {
await replSet.waitUntilRunning();
const uri = await replSet.getUri();
process.env.MONGODB_URI = uri;
};
and my db.ts is like this
// db.ts
export const connect = async () => {
mongoose.set("useFindAndModify", false);
const conn = mongoose.connect(
process.env.MONGODB_URI || config.connectionString, connectionSettings
);
When trying to call it from a test file I do something like this
// test.spec.ts
import db from "./db";
beforeAll(async () => {
await db.connect();
});
afterAll(async (done) => {
await db.dropCollections();
await db.disconnect(done);
});
beforeEach(async () => {
await seed();
});
describe('Some test', () => {
it('Should not fail and get the seeders', () => {
// some random tests using the seeds values
})
})
What I read in that post is instead of using globalSetup use a setupFile that will run for every test instead of one globally and then I MIGHT be able to solve the concurrency issue I have with my tests.
So, to conclude, does anyone knows if there is a proper way to configure the mongodb in memory or if I am doing something THAT BAD that is allowing this to happend or if is there any kind of improvement I can do that will prevent "mongodb memory server cannot perform operation: a background operation is currently running for collection" this to happen?

Wait for DB initialization before running Jest tests

I have to add some features, and their corresponding tests, to an API backend I developed some months ago using NodeJS. Checking the test cases I wrote back then, I've come across with this code:
index-route.spec.js
const app = require('../../app');
const request = require('supertest');
function delay() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, 3000);
});
}
beforeAll(async () => {
await delay();
});
// START TESTS
The reason of this delay is to give time to the application to initialize the database connection (I use the Sequelize ORM). This is done in a file (db.js) required from the application main file (app.js):
db.js
async function run() {
try {
// Creates database if it doesn't exist
await DB_init();
// Test database connection
await DB_test();
// Connection was OK, sync tables and relationships
await DB_sync();
// Check if the DB is empty and, in this case, fill the basic data
await DB_init_values();
} catch(error => {
log.error("Couldn't connect to the database '"+dbName+"':", error);
process.exit(1);
}
}
// Init the DB
run();
This works, but now that I have to add more tests, I'd like to refactor this code to avoid the added artificial delay. How could I wait for the database initialization code to be finished before starting the tests? It seems that it's not possible to require a module synchronally, is it?
Cheers,

How to use jest.each asynchronously

I am having problems loading filenames into jest.each asynchronously.
My code:
let files: string[][]
function getFilesWorking() {
files = [["test1"], ["test2"]]
}
async function getFilesAsync() {
files = await Promise.resolve([["test1"], ["test2"]])
}
beforeAll(() => {
console.log("before")
})
describe.only("Name of the group", () => {
getFilesAsync()
test.each(files)("runs", f => {})
})
beforeAll is executed before each test but NOT before initialization of test.each, so I end up with undefined.
How can I load files before using test.each?
You can pass an async callback to beforeAll and await getFilesAsync within it
beforeAll(async () => {
await getFilesAsync();
})
As of Jest 28.1.3 and prior, this is not possible. There is an open issue documenting this behavior.
The best thing you can do for now is put your tests in a regular it() test and do a deep value comparison:
it('tests an array of cases', async () => {
const data = await getSomeAsyncData()
const expectedData = [ ... ]
expect(data).toEqual(expectedData)
})
You can use beforeEach to set up code that will run prior to tests for any given scope, https://jestjs.io/docs/setup-teardown:
beforeEach(() => {
console.log('before every test');
});
describe.only(('Name of the group') => {
beforeEach(() => {
console.log('before tests in this describe block');
})
})
Jest is only going to run the tests in your describe.only block. If you want to use beforeEach in other blocks and run those tests as well, change describe.only to describe.
(Edit: I know this is a year late, I'm just trying to look for a similar problem/solution set and thought I could answer this.)

chromeLauncher/Lighthouse: Mocking chained dependencies that return promises using Jest

I have an application that includes a Node script that runs Lighthouse v3 programatically and headlessly (Lighthouse documentation) to test the application's performance.
I wrote some Jest tests for this which pass locally. The tests are not testing Lighthouse itself, but rather how the Node script deals with with the results data returned from Lighthouse. Therefore, the Lighthouse dependency must be mocked.
In the course of doing my testing I have discovered that chrome-launcher, which is invoked by Lighthouse, launches when I'm doing my Jest test. This means I am not mocking this dependency correctly. I thought it was enough to mock Lighthouse and have done so but I am confused about how I can mock the 'thennable' chrome launcher function function.
The launchChromeAndRunLighthouse function below is from the Lighthouse Node documentation.
lighthouse.js
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
function launchChromeAndRunLighthouse(url, opts, lConfig = null) {
return chromeLauncher
.launch({ chromeFlags: opts.chromeFlags })
.then(chrome => {
const options = { ...opts, port: chrome.port };
return lighthouse(url, options, lConfig).then(results =>
chrome.kill().then(() => results.lhr),
);
});
}
function myFunc(config) {
launchChromeAndRunLighthouse(myAppUrl, config);
// manipulates data returned from launchChromeAndRunLighthouse
return true;
}
module.exports = myFunc;
lighthouse.test.js
import myFunc from './lighthouse';
jest.mock('lighthouse', () =>
jest.fn().mockResolvedValue({
lhr: {
categories: {
performance: {
id: 'performance',
score: 1,
}
},
},
}),
);
// chromeLauncher actually gets invoked by this
describe('myfunc', () => {
it('tests myfunc', async () => {
const result = await myFunc(config);
expect(result).toEqual(true);
});
});
New to Jest and confused at how I can mock chromeLauncher so it's prevented from launching. Thanks
You don't need to mock chromeLauncher, leave it work like it does. Just mocking lighthouse is enough to make tests run and pass. It works as expected in my projects.

Resources