I'm trying to create tests for api created with express and typescript but I get some weired erros In compilation and mock configure, follow my files:
/test/unit/services/episodeService.spec.ts
/*some imports*/
describe('Episodes Service', () => {
let episodesQueryServiceMock: jest.Mocked<typeof episodesQueryService>
let watchTimeInstance: jest.Mocked<WatchTimeInstance>
beforeEach(() =>{
jest.mock('../../../src/services/queries/episodesQueryService')
jest.mock('../../../src/models/')
jest.mock('../../../src/models/WatchTime')
episodesQueryServiceMock = episodesQueryService as jest.Mocked<typeof episodesQueryService>
})
it('When has no watchTime then create it', async ()=> {
const watchTime: WatchTime = watchTimeFactory.build()
(episodesQueryServiceMock.findByUserIdAndEpisodeId as jest.Mock).mockReturnValue(watchTime)
(watchTimeInstance.save as jest.Mock).mockReturnValue(watchTime)
const actual = await episodesService.setWatchTime(watchTime)
})
})
in this code I have a error in first line my it
This expression is not callable. Type 'WatchTime' has no call signatures.ts(2349)
this line I try to use a factory to create a random objetc to tes using factory.ts lib, follow my factory:
./test/factories/watchTime.ts
/*some imports*/
export const watchTimeFactory = Factory.makeFactory<WatchTime>(() => ({
userId: Number(faker.random.numeric()),
episodeId: Number(faker.random.numeric()),
seconds: Number(faker.random.numeric()),
createdAt: new Date(),
updatedAt: new Date()
}))
I thought who this was a lib bug, but I got same error using two factories libs ( Factory.ts and fishery, both use amoust same config to build factories), but if I put my factory call out of it, with my mock creations the error not happen but I got another error when I run my test
TypeError: episodesQueryServiceMock.findByUserIdAndEpisodeId.mockReturnValue is not a function
follow my jest configs
./jest.config.ts
import type {Config} from '@jest/types';
// Sync object
const config: Config.InitialOptions = {
verbose: true,
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
setupFiles: ['<rootDir>/test/setup.ts'],
testEnvironment: 'node',
maxWorkers: 1,
preset: 'ts-jest'
};
export default config;
./test/setup.ts
import dotenv from 'dotenv'
dotenv.config()
process.env.DATABASE = process.env.DATABASE?.replace(/_development/, '_test')
I think my errors is something about config, please help me
thank you
I found my errors, have two problems in my code:
1st) My factory code it's wrong, the correct code is:
import * as Factory from "factory.ts";
import { faker } from '@faker-js/faker'
import { WatchTime } from '../../src/models'
export const watchTimeFactory = Factory.makeFactory<WatchTime>({
userId: Number(faker.random.numeric()),
episodeId: Number(faker.random.numeric()),
seconds: Number(faker.random.numeric()),
createdAt: new Date(),
updatedAt: new Date()
});
2nd) My jest.mock() is in a wrong context, for work I need put all in same context how are my imports:
import { episodesService } from "../../../src/services/episodesService"
import { episodesQueryService } from "../../../src/services/queries/episodesQueryService"
import { WatchTime } from "../../../src/models/"
import { WatchTimeInstance } from "../../../src/models/WatchTime"
import { watchTimeFactory } from "../../factories/watchTime"
// this place
jest.mock('../../../src/services/queries/episodesQueryService')
jest.mock('../../../src/models/')
jest.mock('../../../src/models/WatchTime')
describe('Episodes Service', () => {
it('When has no watchTime then create it', async ()=> {
const watchTime = watchTimeFactory.build();
(episodesQueryService.findByUserIdAndEpisodeId as jest.Mock).mockReturnValue(watchTime);
const actual = await episodesService.setWatchTime(watchTime);
})
})
this steps solve my problems, thank you