|
| 1 | +import { spawnSync } from 'child_process'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as os from 'os'; |
| 4 | +import * as path from 'path'; |
| 5 | +import { JSONSchemaForNPMPackageJsonFiles } from '@schemastore/package'; |
| 6 | +import { create } from 'ts-node'; |
| 7 | +import { detectJestVersion } from '../detectJestVersion'; |
| 8 | + |
| 9 | +const compileFnCode = (pathToFn: string) => { |
| 10 | + const fnContents = fs.readFileSync(pathToFn, 'utf-8'); |
| 11 | + |
| 12 | + return create({ |
| 13 | + transpileOnly: true, |
| 14 | + compilerOptions: { sourceMap: false }, |
| 15 | + }).compile(fnContents, pathToFn); |
| 16 | +}; |
| 17 | +const compiledFn = compileFnCode(require.resolve('../detectJestVersion.ts')); |
| 18 | +const relativePathToFn = 'eslint-plugin-jest/lib/rules/detectJestVersion.js'; |
| 19 | + |
| 20 | +const runNodeScript = (cwd: string, script: string) => { |
| 21 | + return spawnSync('node', ['-e', script.split('\n').join(' ')], { |
| 22 | + cwd, |
| 23 | + encoding: 'utf-8', |
| 24 | + }); |
| 25 | +}; |
| 26 | + |
| 27 | +const runDetectJestVersion = (cwd: string) => { |
| 28 | + return runNodeScript( |
| 29 | + cwd, |
| 30 | + ` |
| 31 | + try { |
| 32 | + console.log(require('${relativePathToFn}').detectJestVersion()); |
| 33 | + } catch (error) { |
| 34 | + console.error(error.message); |
| 35 | + } |
| 36 | + `, |
| 37 | + ); |
| 38 | +}; |
| 39 | + |
| 40 | +/** |
| 41 | + * Makes a new temp directory, prefixed with `eslint-plugin-jest-` |
| 42 | + * |
| 43 | + * @return {Promise<string>} |
| 44 | + */ |
| 45 | +const makeTempDir = () => |
| 46 | + fs.mkdtempSync(path.join(os.tmpdir(), 'eslint-plugin-jest-')); |
| 47 | + |
| 48 | +interface ProjectStructure { |
| 49 | + [key: `${string}/package.json`]: JSONSchemaForNPMPackageJsonFiles; |
| 50 | + [key: `${string}/${typeof relativePathToFn}`]: string; |
| 51 | + [key: `${string}/`]: null; |
| 52 | + 'package.json'?: JSONSchemaForNPMPackageJsonFiles; |
| 53 | +} |
| 54 | + |
| 55 | +const setupFakeProject = (structure: ProjectStructure): string => { |
| 56 | + const tempDir = makeTempDir(); |
| 57 | + |
| 58 | + for (const [filePath, contents] of Object.entries(structure)) { |
| 59 | + if (contents === null) { |
| 60 | + fs.mkdirSync(path.join(tempDir, filePath), { recursive: true }); |
| 61 | + |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + const folderPath = path.dirname(filePath); |
| 66 | + |
| 67 | + // make the directory (recursively) |
| 68 | + fs.mkdirSync(path.join(tempDir, folderPath), { recursive: true }); |
| 69 | + |
| 70 | + const finalContents = |
| 71 | + typeof contents === 'string' ? contents : JSON.stringify(contents); |
| 72 | + |
| 73 | + fs.writeFileSync(path.join(tempDir, filePath), finalContents); |
| 74 | + } |
| 75 | + |
| 76 | + return tempDir; |
| 77 | +}; |
| 78 | + |
| 79 | +describe('detectJestVersion', () => { |
| 80 | + describe('basic tests', () => { |
| 81 | + const packageJsonFactory = jest.fn<JSONSchemaForNPMPackageJsonFiles, []>(); |
| 82 | + |
| 83 | + beforeEach(() => { |
| 84 | + jest.resetModules(); |
| 85 | + jest.doMock(require.resolve('jest/package.json'), packageJsonFactory); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('when the package.json is missing the version property', () => { |
| 89 | + it('throws an error', () => { |
| 90 | + packageJsonFactory.mockReturnValue({}); |
| 91 | + |
| 92 | + expect(() => detectJestVersion()).toThrow( |
| 93 | + /Unable to detect Jest version/iu, |
| 94 | + ); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('caches versions', () => { |
| 99 | + packageJsonFactory.mockReturnValue({ version: '1.2.3' }); |
| 100 | + |
| 101 | + const version = detectJestVersion(); |
| 102 | + |
| 103 | + jest.resetModules(); |
| 104 | + |
| 105 | + expect(detectJestVersion).not.toThrow(); |
| 106 | + expect(detectJestVersion()).toBe(version); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('when in a simple project', () => { |
| 111 | + it('finds the correct version', () => { |
| 112 | + const projectDir = setupFakeProject({ |
| 113 | + 'package.json': { name: 'simple-project' }, |
| 114 | + [`node_modules/${relativePathToFn}` as const]: compiledFn, |
| 115 | + 'node_modules/jest/package.json': { |
| 116 | + name: 'jest', |
| 117 | + version: '21.0.0', |
| 118 | + }, |
| 119 | + }); |
| 120 | + |
| 121 | + const { stdout, stderr } = runDetectJestVersion(projectDir); |
| 122 | + |
| 123 | + expect(stdout.trim()).toBe('21'); |
| 124 | + expect(stderr.trim()).toBe(''); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('when in a hoisted mono-repo', () => { |
| 129 | + it('finds the correct version', () => { |
| 130 | + const projectDir = setupFakeProject({ |
| 131 | + 'package.json': { name: 'mono-repo' }, |
| 132 | + [`node_modules/${relativePathToFn}` as const]: compiledFn, |
| 133 | + 'node_modules/jest/package.json': { |
| 134 | + name: 'jest', |
| 135 | + version: '19.0.0', |
| 136 | + }, |
| 137 | + 'packages/a/package.json': { name: 'package-a' }, |
| 138 | + 'packages/b/package.json': { name: 'package-b' }, |
| 139 | + }); |
| 140 | + |
| 141 | + const { stdout, stderr } = runDetectJestVersion(projectDir); |
| 142 | + |
| 143 | + expect(stdout.trim()).toBe('19'); |
| 144 | + expect(stderr.trim()).toBe(''); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + describe('when in a subproject', () => { |
| 149 | + it('finds the correct versions', () => { |
| 150 | + const projectDir = setupFakeProject({ |
| 151 | + 'backend/package.json': { name: 'package-a' }, |
| 152 | + [`backend/node_modules/${relativePathToFn}` as const]: compiledFn, |
| 153 | + 'backend/node_modules/jest/package.json': { |
| 154 | + name: 'jest', |
| 155 | + version: '24.0.0', |
| 156 | + }, |
| 157 | + 'frontend/package.json': { name: 'package-b' }, |
| 158 | + [`frontend/node_modules/${relativePathToFn}` as const]: compiledFn, |
| 159 | + 'frontend/node_modules/jest/package.json': { |
| 160 | + name: 'jest', |
| 161 | + version: '15.0.0', |
| 162 | + }, |
| 163 | + }); |
| 164 | + |
| 165 | + const { stdout: stdoutBackend, stderr: stderrBackend } = |
| 166 | + runDetectJestVersion(path.join(projectDir, 'backend')); |
| 167 | + |
| 168 | + expect(stdoutBackend.trim()).toBe('24'); |
| 169 | + expect(stderrBackend.trim()).toBe(''); |
| 170 | + |
| 171 | + const { stdout: stdoutFrontend, stderr: stderrFrontend } = |
| 172 | + runDetectJestVersion(path.join(projectDir, 'frontend')); |
| 173 | + |
| 174 | + expect(stdoutFrontend.trim()).toBe('15'); |
| 175 | + expect(stderrFrontend.trim()).toBe(''); |
| 176 | + }); |
| 177 | + }); |
| 178 | + |
| 179 | + describe('when jest is not installed', () => { |
| 180 | + it('throws an error', () => { |
| 181 | + const projectDir = setupFakeProject({ |
| 182 | + 'package.json': { name: 'no-jest' }, |
| 183 | + [`node_modules/${relativePathToFn}` as const]: compiledFn, |
| 184 | + 'node_modules/pack/package.json': { name: 'pack' }, |
| 185 | + }); |
| 186 | + |
| 187 | + const { stdout, stderr } = runDetectJestVersion(projectDir); |
| 188 | + |
| 189 | + expect(stdout.trim()).toBe(''); |
| 190 | + expect(stderr.trim()).toContain('Unable to detect Jest version'); |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + describe('when jest is changed on disk', () => { |
| 195 | + it('uses the cached version', () => { |
| 196 | + const projectDir = setupFakeProject({ |
| 197 | + 'package.json': { name: 'no-jest' }, |
| 198 | + [`node_modules/${relativePathToFn}` as const]: compiledFn, |
| 199 | + 'node_modules/jest/package.json': { name: 'jest', version: '26.0.0' }, |
| 200 | + }); |
| 201 | + |
| 202 | + const { stdout, stderr } = runNodeScript( |
| 203 | + projectDir, |
| 204 | + ` |
| 205 | + const { detectJestVersion } = require('${relativePathToFn}'); |
| 206 | + const fs = require('fs'); |
| 207 | + |
| 208 | + console.log(detectJestVersion()); |
| 209 | + fs.writeFileSync( |
| 210 | + 'node_modules/jest/package.json', |
| 211 | + JSON.stringify({ |
| 212 | + name: 'jest', |
| 213 | + version: '25.0.0', |
| 214 | + }), |
| 215 | + ); |
| 216 | + console.log(detectJestVersion()); |
| 217 | + `, |
| 218 | + ); |
| 219 | + |
| 220 | + const [firstCall, secondCall] = stdout.split('\n'); |
| 221 | + |
| 222 | + expect(firstCall).toBe('26'); |
| 223 | + expect(secondCall).toBe('26'); |
| 224 | + expect(stderr.trim()).toBe(''); |
| 225 | + }); |
| 226 | + }); |
| 227 | +}); |
0 commit comments