|
1 | 1 | import { LogLevels } from 'bs-logger' |
2 | 2 | import { writeFileSync } from 'fs' |
3 | 3 | import { removeSync } from 'fs-extra' |
| 4 | +import { normalize } from 'path' |
4 | 5 |
|
5 | 6 | import { makeCompiler } from '../__helpers__/fakers' |
6 | 7 | import { logTargetMock } from '../__helpers__/mocks' |
7 | 8 | import { tempDir } from '../__helpers__/path' |
8 | 9 | import ProcessedSource from '../__helpers__/processed-source' |
9 | 10 |
|
| 11 | +import * as compilerUtils from './compiler-utils' |
| 12 | + |
10 | 13 | const logTarget = logTargetMock() |
11 | 14 |
|
12 | 15 | const baseTsJestConfig = { |
@@ -107,6 +110,82 @@ describe('cache', () => { |
107 | 110 | }) |
108 | 111 | }) |
109 | 112 |
|
| 113 | +describe('cache resolved modules for test file', () => { |
| 114 | + let spy: jest.SpyInstance |
| 115 | + const fileName = 'src/__mocks__/main.spec.ts' |
| 116 | + const source = JSON.stringify(require('../__mocks__/main.spec')) |
| 117 | + |
| 118 | + describe('with program', () => { |
| 119 | + beforeEach(() => { |
| 120 | + // tslint:disable-next-line:no-empty |
| 121 | + spy = jest.spyOn(compilerUtils, 'cacheResolvedModules').mockImplementationOnce(() => {}) |
| 122 | + }) |
| 123 | + |
| 124 | + afterEach(() => { |
| 125 | + spy.mockRestore() |
| 126 | + }) |
| 127 | + |
| 128 | + it('should cache resolved modules for test file with testMatchPatterns from jest config when match', () => { |
| 129 | + const tmp = tempDir('compiler') |
| 130 | + const compiler = makeCompiler({ |
| 131 | + jestConfig: { cache: true, cacheDirectory: tmp, testRegex: [/.*\.(spec|test)\.[jt]sx?$/] as any[] }, |
| 132 | + tsJestConfig: { tsConfig: false, compilerHost: true, incremental: false }, |
| 133 | + }) |
| 134 | + compiler.compile(source, fileName) |
| 135 | + |
| 136 | + expect(spy).toHaveBeenCalled() |
| 137 | + expect(spy.mock.calls[0][0]).toEqual(normalize(fileName)) |
| 138 | + expect(spy.mock.calls[0][1]).toEqual(source) |
| 139 | + }) |
| 140 | + |
| 141 | + it(`shouldn't cache resolved modules for test file with testMatchPatterns from jest config when not match`, () => { |
| 142 | + const tmp = tempDir('compiler') |
| 143 | + const compiler = makeCompiler({ |
| 144 | + jestConfig: { cache: true, cacheDirectory: tmp, testRegex: [/.*\.(foo|bar)\.[jt]sx?$/] as any[] }, |
| 145 | + tsJestConfig: { tsConfig: false, compilerHost: true, incremental: false }, |
| 146 | + }) |
| 147 | + compiler.compile(source, fileName) |
| 148 | + |
| 149 | + expect(spy).not.toHaveBeenCalled() |
| 150 | + }) |
| 151 | + }) |
| 152 | + |
| 153 | + describe('with incremental program', () => { |
| 154 | + beforeEach(() => { |
| 155 | + // tslint:disable-next-line:no-empty |
| 156 | + spy = jest.spyOn(compilerUtils, 'cacheResolvedModules').mockImplementationOnce(() => {}) |
| 157 | + }) |
| 158 | + |
| 159 | + afterEach(() => { |
| 160 | + spy.mockRestore() |
| 161 | + }) |
| 162 | + |
| 163 | + it('should cache resolved modules for test file with testMatchPatterns from jest config when match', () => { |
| 164 | + const tmp = tempDir('compiler') |
| 165 | + const compiler = makeCompiler({ |
| 166 | + jestConfig: { cache: true, cacheDirectory: tmp, testRegex: [/.*\.(spec|test)\.[jt]sx?$/] as any[] }, |
| 167 | + tsJestConfig: { tsConfig: false, compilerHost: true, incremental: true }, |
| 168 | + }) |
| 169 | + compiler.compile(source, fileName) |
| 170 | + |
| 171 | + expect(spy).toHaveBeenCalled() |
| 172 | + expect(spy.mock.calls[0][0]).toEqual(normalize(fileName)) |
| 173 | + expect(spy.mock.calls[0][1]).toEqual(source) |
| 174 | + }) |
| 175 | + |
| 176 | + it(`shouldn't cache resolved modules for test file with testMatchPatterns from jest config when not match`, () => { |
| 177 | + const tmp = tempDir('compiler') |
| 178 | + const compiler = makeCompiler({ |
| 179 | + jestConfig: { cache: true, cacheDirectory: tmp, testRegex: [/.*\.(foo|bar)\.[jt]sx?$/] as any[] }, |
| 180 | + tsJestConfig: { tsConfig: false, compilerHost: true, incremental: true }, |
| 181 | + }) |
| 182 | + compiler.compile(source, fileName) |
| 183 | + |
| 184 | + expect(spy).not.toHaveBeenCalled() |
| 185 | + }) |
| 186 | + }) |
| 187 | +}) |
| 188 | + |
110 | 189 | describe('allowJs', () => { |
111 | 190 | const baseFileName = 'test-allowJs' |
112 | 191 | const baseFileExt = 'test.js' |
|
0 commit comments