/
compiler-utils.spec.ts
67 lines (54 loc) · 2.29 KB
/
compiler-utils.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
import { logger } from 'bs-logger'
import * as fs from 'fs'
import { resolve } from 'path'
import { makeCompiler } from '../__helpers__/fakers'
import { tempDir } from '../__helpers__/path'
import { MemoryCache, TSFile } from '../types'
import { cacheResolvedModules, getResolvedModulesCache } from './compiler-utils'
const memoryCache: MemoryCache = {
contents: Object.create(null),
versions: Object.create(null),
outputs: Object.create(null),
resolvedModules: Object.create(null),
files: new Map<string, TSFile>(),
}
describe('cacheResolvedModules', () => {
let spy: jest.SpyInstance<void, any[]>
beforeAll(() => {
// tslint:disable-next-line:no-empty
spy = jest.spyOn(fs, 'writeFileSync')
})
beforeEach(() => {
memoryCache.resolvedModules = Object.create(null)
})
afterEach(() => {
spy.mockRestore()
})
it('should store resolved modules in memory cache and file system when there are resolved modules', () => {
const tmp = tempDir('compiler')
const compiler = makeCompiler({
jestConfig: { cache: true, cacheDirectory: tmp },
tsJestConfig: { tsConfig: false },
})
const fileName = 'src/__mocks__/main.spec.ts'
const source = JSON.stringify(require('../__mocks__/main.spec'))
compiler.compile(source, fileName)
cacheResolvedModules(fileName, source, memoryCache, compiler.program!, tmp, logger)
expect(memoryCache.resolvedModules[fileName].modulePaths).toContain(resolve('src/__mocks__/main.ts'))
expect(memoryCache.resolvedModules[fileName].testFileContent).toEqual(source)
expect(spy).toHaveBeenCalledWith(getResolvedModulesCache(tmp), JSON.stringify(memoryCache.resolvedModules))
})
it(`should store resolved modules in memory cache but not file system when there aren't resolved modules`, () => {
const tmp = tempDir('compiler')
const compiler = makeCompiler({
jestConfig: { cache: true, cacheDirectory: tmp },
tsJestConfig: { tsConfig: false },
})
const fileName = 'src/__mocks__/thing.spec.ts'
const source = JSON.stringify(require('../__mocks__/thing.spec'))
compiler.compile(source, fileName)
cacheResolvedModules(fileName, source, memoryCache, compiler.program!, tmp, logger)
expect(memoryCache.resolvedModules[fileName]).toBeUndefined()
expect(spy).not.toHaveBeenCalled()
})
})