jest-each tear down is not called - jestjs

I am trying use beforeEach and afterEach for test.each, seems these set up and tear down are not called, here is my sample code
let mydata;
describe('test each feature of jest %re', () => {
beforeAll( () => {
return mydata = [1,2,5,7] ;
});
dataArr = [1,2];
it.each(dataArr)
('%i', async(data) => {
expect(data).toBe(1);
});
});
I'm getting error
● test each feature of jest %re › encountered a declaration exception

Related

Jest test.each not waiting for data to be available

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));
});
});

Nodejs, Serverless expected stub function to be called once but was called 0 times

I have been the said error when trying to create a stub from sinon in my test function. I am trying to test a function responsible to make http calls to other endpoints. I am trying to understand why Its not resolving to the output provided.
const sinon = require('sinon');
const sandbox = sinon.createSandbox();
describe('test endpoint', () => {
it('should be test function', async () => {
const stub = sinon.stub(someServiceMock.POST, '/funcName').resolves({ status: 204 });
sinon.assert.calledOnce(stub);
});
});
});
and getting AssertError: expected '/funcName' to be called once but was called 0 times
The object i pass in the stub is
const someServiceMock = {
POST: {
'/funcName': () => {},
},
};
The stubbed function is never called in the code-sample you provided. If you actually call the funtion with
describe('test endpoint', () => {
it('should be test function', async () => {
const stub = sinon.stub(someServiceMock.POST, '/funcName').resolves({ status: 204 });
someServiceMock.POST["/funcName"]();
sinon.assert.calledOnce(stub);
});
});
the test should pass as expected.

Nodejs Jest mocking is not working in multiple describe block in a single file, it works in first describe block only

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

Context in mocha test is undefined

So I'm using mocha and node to test some apis. I have a test that goes
import { describe, before, it, xit } from 'mocha';
describe('test my scenarios dude', () => {
before('do all my pre-test stuff', () => {
const blah = blah;
});
it('tests my really useful test', () => {
const testName = this.test.ctx.currentTest.fullTitle();
});
});
The 'this' is undefined though. How can I get the test name?
https://mochajs.org/#arrow-functions
as docs says Passing arrow functions (“lambdas”) to Mocha is discouraged
use function instead
describe('test my scenarios dude', function() {
before('do all my pre-test stuff', function() {
const blah = blah;
});
it('tests my really useful test', function() {
const testName = this.test.ctx.currentTest.fullTitle();
});
});
also you can read more about arrow functions here. they don't have this

Issues creating a Unit Test stub for this function return using Sinon and Should

I have the following health.js file..
exports.endpoint = function (req, res) {
let http_status = 200;
res.sendStatus(http_status);
};
Where I'm trying to figure out how I can create a Unit Test from this endpoint.
Below is my attempt so far..
import { sinon, should } from "../../test-harness";
let health = require('../../../src/endpoints/health');
health.endpoint = sinon.stub();
describe('The health endpoint', function () {
it('should call .endpoint()', () => {
health.endpoint.should.have.been.called;
});
});
But it seems to be passing regardless of if I add a .not to my should.have.been.called statement.
Where am I going wrong?

Resources