I have this simple nodeJS application
const express = require('express')
const app = express()
const port = 3000
import {testFunction} from './util'
app.get('/', (req, res) => {
console.log(testFunction(), 'result of testFunction')
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
export { app};
This is my jest tests:
import request from 'supertest'
describe('test cases', () => {
const mockTestFunction = jest.fn()
beforeEach(() => {
jest.clearAllMocks() // <---------- it doesn't help to clear
jest.restoreAllMocks()
mockTestFunction.mockClear()
mockTestFunction.mockRestore()
})
it('1', async () => {
jest.mock('../util', () => ({
testFunction: mockTestFunction
}))
mockTestFunction.mockReturnValue('test 1')
const {app} = await import('../expressApp')
return request(app)
.get(`/`)
.expect(200)
.then((response) => {})
})
it('2', async () => {
const {app} = await import('../expressApp')
return request(app)
.get(`/`)
.expect(200)
.then((response) => {}) // here I always get mocked "test 1"
})
})
My purpose is clear mockTestFunction for test "2", but as a result, I get always
"'test 1 result of testFunction'", it mocked it first test case and I can't restore it. I want to restore mockTestFunction to original (non-mocked) implementation, it should return "'result of testFunction'"
from Jest clear mocks between test cases (javascript)
No comments:
Post a Comment