beforeEach(async () => {
const sandbox = sinon.sandbox.create()
...
})
test('/add', () => {
// how can I use sandbox here?
})
What I need is something like t.context in ava
Just declare sandbox so it is available in the scope of beforeEach and test:
let sandbox;
beforeEach(async () => {
sandbox = sinon.sandbox.create()
...
})
test('/add', () => {
// sandbox available for use
})
Related
I'm going around in circles trying to get a data driven test in Jest working. Although tests run async I was expecting the describes to run synchronously so the data would be set up before the main test runs. I also tried a beforeAll but this has the same problem. Is there a way to do this?
describe("My tests"), () => {
let testData = Array<MyDataStructure> = [];
describe("prepare test data", () => {
getData.then((data) => {
testData = data;
});
});
describe("run tests", () => {
test.each(testData)("this fails as testData is empty array", row: MyDataStructure) => console.log(row);
});
});
});
Wait until getData is done then execute the test cases.
beforeAll for getting data once for all tests.
beforeEach for re-fetch data for each test:
describe("My tests", () => {
let testData: Array<MyDataStructure> = [];
beforeAll(async () => { // async function
testData = await getData(); // wait until getData is done
});
describe("run tests", () => {
test.each(testData)("this fails as testData is empty array", (row: MyDataStructure) => console.log(row));
});
});
I'm using react-easy-state and I want to reset the store on each test. Which works fine. But when I try to mock a local module the mocked function does not get called. The mocking works fine without the use of isolateModules.
How do I do mocking on a module in conjunction with isolateModules?
This is what I'm trying to do:
beforeEach(() => {
jest.doMock('./util/getValidPosition', () => {
return { getValidPosition: jest.fn() };
});
jest.isolateModules(() => {
store = require('./store').workspace;
});
});
afterEach(() => {
jest.resetModules();
});
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.)
I am using jest for unit testing in my node express application,please excuse , because i am new to all this
in my abc.test.js
const s3Helper = require('../../../../../lib/s3_helper');
beforeEach(async () => {
s3Helper.uploadBufferToS3 = jest.fn(() => true);
});
describe('test1', () => {
it('test1', async () => {
expect(s3Helper.uploadBufferToS3)
.toHaveBeenCalled();
});
});
describe('test2', () => {
it('test2', async () => {
expect(s3Helper.uploadBufferToS3)
.toHaveBeenCalled();
});
});
so when i run this test file in test1 it returns that test is passed, however in test2 it returns expected >=1 returned 0.
since i am mocking it beforeEach i expect it should return 1 for each describe block
I'm writing tests for a simple, mongoose-based service:
describe('users service', () => {
const service = app.service('users');
it('registered the service', () => {
assert.ok(service, 'Registered the service');
});
});
The problem is, how do I clean the data before each test?
Right now I have:
// Reset service state before each test.
beforeEach((done) => {
service.find().then((items) => {
items.data.forEach((item) => {
service.remove(item._id)
});
done();
})
});
This is awkward. Is there a better way?
With the latest adapter version you can also temporarily set the multi option to allow removing all entries and call .remove(null):
// Reset service state before each test.
beforeEach(async () => {
const multi = service.options.multi;
service.options.multi = 'remove';
await service.remove(null);
service.options.multi = multi;
});
This will also wait until everything is removed (which your example wouldn't do).