/
index.spec.ts
96 lines (90 loc) · 3.35 KB
/
index.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// tslint:disable:max-line-length
import * as tsJest from '.'
import { TsJestTransformer } from './ts-jest-transformer'
jest.mock('./ts-jest-transformer', () => {
class TsJestTransformer {
process: jest.Mock = jest.fn()
getCacheKey: jest.Mock = jest.fn()
constructor(public opt?: any) {}
}
return { TsJestTransformer }
})
jest.mock('./config/create-jest-preset', () => ({
createJestPreset: () => ({ jestPreset: true }),
}))
describe('ts-jest', () => {
it('should export a `createTransformer` function', () => {
expect(typeof tsJest.createTransformer).toBe('function')
})
it('should export a `process` function', () => {
expect(typeof tsJest.process).toBe('function')
})
it('should export a `getCacheKey` function', () => {
expect(typeof tsJest.getCacheKey).toBe('function')
})
it('should export a `createJestPreset` function', () => {
expect(typeof tsJest.createJestPreset).toBe('function')
})
it('should export a `mocked` function', () => {
expect(typeof tsJest.mocked).toBe('function')
})
it('should export a `pathsToModuleNameMapper` function', () => {
expect(typeof tsJest.pathsToModuleNameMapper).toBe('function')
})
it('should export a `jestPreset` object', () => {
expect(tsJest.jestPreset).toEqual({ jestPreset: true })
})
})
describe('old entry point', () => {
const MANIFEST = { tsJestIndex: true }
const spy = jest.spyOn(console, 'warn')
spy.mockImplementation(() => undefined)
afterAll(() => {
spy.mockRestore()
})
it('should warn when using old path to ts-jest', () => {
jest.mock('../dist/index', () => MANIFEST)
expect(require('../preprocessor.js')).toBe(MANIFEST)
expect(spy).toHaveBeenCalledTimes(1)
expect(spy.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"ts-jest[main] (WARN) Replace any occurrences of \\"ts-jest/dist/preprocessor.js\\" or \\"<rootDir>/node_modules/ts-jest/preprocessor.js\\" in the 'transform' section of your Jest config with just \\"ts-jest\\".",
]
`)
})
})
describe('createTransformer', () => {
it('should create different instances', () => {
const tr1 = tsJest.createTransformer()
const tr2 = tsJest.createTransformer()
expect(tr1).toBeInstanceOf(TsJestTransformer)
expect(tr2).toBeInstanceOf(TsJestTransformer)
expect(tr1).not.toBe(tr2)
})
it('should accept base options', () => {
expect((tsJest.createTransformer({ isolatedModules: true }) as any).opt).toEqual({
isolatedModules: true,
})
})
})
describe('bare methods', () => {
beforeEach(() => {
tsJest.__resetModule()
})
it('should use the same instance of transformer for `process`', () => {
tsJest.process('foo')
expect(tsJest.__singleton().process).toHaveBeenCalledTimes(1)
expect(tsJest.__singleton().process).toHaveBeenCalledWith('foo')
tsJest.process('bar')
expect(tsJest.__singleton().process).toHaveBeenCalledTimes(2)
expect(tsJest.__singleton().process).toHaveBeenLastCalledWith('bar')
})
it('should use the same instance of transformer for `getCacheKey`', () => {
tsJest.getCacheKey('foo')
expect(tsJest.__singleton().getCacheKey).toHaveBeenCalledTimes(1)
expect(tsJest.__singleton().getCacheKey).toHaveBeenCalledWith('foo')
tsJest.getCacheKey('bar')
expect(tsJest.__singleton().getCacheKey).toHaveBeenCalledTimes(2)
expect(tsJest.__singleton().getCacheKey).toHaveBeenLastCalledWith('bar')
})
})