From 1faf93d71a6ee15c07f84967a2466e4d53e834d9 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Tue, 23 Nov 2021 17:30:12 -0800 Subject: [PATCH] [wip] Fix unit tests on windows --- .eslintrc.js | 4 ++++ .../storyshots-core/src/frameworks/configure.test.ts | 9 +++++---- app/react/src/server/cra-config.test.ts | 9 +++++---- .../src/utils/__tests__/server-statics.test.ts | 11 ++++++++--- package.json | 1 + yarn.lock | 8 ++++++++ 6 files changed, 31 insertions(+), 11 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 3dc077d3aad4..b38e16c9be69 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -3,6 +3,10 @@ module.exports = { extends: ['@storybook/eslint-config-storybook', 'plugin:storybook/recommended'], rules: { '@typescript-eslint/ban-ts-comment': 'warn', + 'jest/no-standalone-expect': [ + 'error', + { additionalTestBlockFunctions: ['it.skipWindows', 'it.onWindows'] }, + ], }, overrides: [ { diff --git a/addons/storyshots/storyshots-core/src/frameworks/configure.test.ts b/addons/storyshots/storyshots-core/src/frameworks/configure.test.ts index 76c88463876a..509aa5a13b24 100644 --- a/addons/storyshots/storyshots-core/src/frameworks/configure.test.ts +++ b/addons/storyshots/storyshots-core/src/frameworks/configure.test.ts @@ -1,3 +1,4 @@ +import path from 'path'; import { getPreviewFile, getMainFile } from './configure'; // eslint-disable-next-line global-require, jest/no-mocks-import @@ -19,9 +20,9 @@ describe('preview files', () => { ${'config.js'} ${'config.jsx'} `('resolves a valid preview file from $filepath', ({ filepath }) => { - setupFiles({ [`test/${filepath}`]: 'true' }); + setupFiles({ [path.join('test', filepath)]: 'true' }); - expect(getPreviewFile('test/')).toEqual(`test/${filepath}`); + expect(getPreviewFile('test/')).toEqual(`test${path.sep}${filepath}`); }); it('returns false when none of the paths exist', () => { @@ -39,9 +40,9 @@ describe('main files', () => { ${'main.js'} ${'main.jsx'} `('resolves a valid main file path from $filepath', ({ filepath }) => { - setupFiles({ [`test/${filepath}`]: 'true' }); + setupFiles({ [path.join('test', filepath)]: 'true' }); - expect(getMainFile('test/')).toEqual(`test/${filepath}`); + expect(getMainFile('test/')).toEqual(`test${path.sep}${filepath}`); }); it('returns false when none of the paths exist', () => { diff --git a/app/react/src/server/cra-config.test.ts b/app/react/src/server/cra-config.test.ts index c6b1a57c30ea..3f5c8c9d361a 100644 --- a/app/react/src/server/cra-config.test.ts +++ b/app/react/src/server/cra-config.test.ts @@ -1,4 +1,5 @@ import fs from 'fs'; +import path from 'path'; import { getReactScriptsPath } from './cra-config'; jest.mock('fs', () => ({ @@ -7,7 +8,7 @@ jest.mock('fs', () => ({ existsSync: jest.fn(() => true), })); -const SCRIPT_PATH = '.bin/react-scripts'; +const SCRIPT_PATH = path.join('.bin', 'react-scripts'); describe('cra-config', () => { describe('when used with the default react-scripts package', () => { @@ -19,7 +20,7 @@ describe('cra-config', () => { it('should locate the react-scripts package', () => { expect(getReactScriptsPath({ noCache: true })).toEqual( - '/test-project/node_modules/react-scripts' + path.join(path.sep, 'test-project', 'node_modules', 'react-scripts') ); }); }); @@ -33,7 +34,7 @@ describe('cra-config', () => { it('should locate the react-scripts package', () => { expect(getReactScriptsPath({ noCache: true })).toEqual( - '/test-project/node_modules/custom-react-scripts' + path.join(path.sep, 'test-project', 'node_modules', 'custom-react-scripts') ); }); }); @@ -65,7 +66,7 @@ exit $ret` it('should locate the react-scripts package', () => { expect(getReactScriptsPath({ noCache: true })).toEqual( - '/test-project/node_modules/custom-react-scripts' + path.join(path.sep, 'test-project', 'node_modules', 'custom-react-scripts') ); }); }); diff --git a/lib/core-server/src/utils/__tests__/server-statics.test.ts b/lib/core-server/src/utils/__tests__/server-statics.test.ts index 411511ce0b05..789d0d2b1940 100644 --- a/lib/core-server/src/utils/__tests__/server-statics.test.ts +++ b/lib/core-server/src/utils/__tests__/server-statics.test.ts @@ -1,5 +1,6 @@ import fs from 'fs-extra'; import path from 'path'; +import 'jest-os-detection'; import { parseStaticDir } from '../server-statics'; fs.pathExists = jest.fn().mockReturnValue(true); @@ -37,30 +38,34 @@ describe('parseStaticDir', () => { }); }); - it('supports absolute file paths', async () => { + it.skipWindows('supports absolute file paths - posix', async () => { await expect(parseStaticDir('/foo/bar')).resolves.toEqual({ staticDir: '/foo/bar', staticPath: '/foo/bar', targetDir: './', targetEndpoint: '/', }); + }); + it.onWindows('supports absolute file paths - windows', async () => { await expect(parseStaticDir('C:\\foo\\bar')).resolves.toEqual({ - staticDir: expect.any(String), // can't test this properly on unix + staticDir: path.resolve('C:\\foo\\bar'), staticPath: path.resolve('C:\\foo\\bar'), targetDir: './', targetEndpoint: '/', }); }); - it('supports absolute file paths with custom endpoint', async () => { + it.skipWindows('supports absolute file paths with custom endpoint - posix', async () => { await expect(parseStaticDir('/foo/bar:/custom-endpoint')).resolves.toEqual({ staticDir: '/foo/bar', staticPath: '/foo/bar', targetDir: './custom-endpoint', targetEndpoint: '/custom-endpoint', }); + }); + it.onWindows('supports absolute file paths with custom endpoint - windows', async () => { await expect(parseStaticDir('C:\\foo\\bar:/custom-endpoint')).resolves.toEqual({ staticDir: expect.any(String), // can't test this properly on unix staticPath: path.resolve('C:\\foo\\bar'), diff --git a/package.json b/package.json index 7a7b5a370fb8..39762525ed6f 100644 --- a/package.json +++ b/package.json @@ -244,6 +244,7 @@ "jest-enzyme": "^7.1.2", "jest-image-snapshot": "^4.3.0", "jest-jasmine2": "^26.6.3", + "jest-os-detection": "^1.3.1", "jest-raw-loader": "^1.0.1", "jest-serializer-html": "^7.0.0", "jest-teamcity": "^1.9.0", diff --git a/yarn.lock b/yarn.lock index 7e5607218f4f..50d09a61d8eb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9044,6 +9044,7 @@ __metadata: jest-enzyme: ^7.1.2 jest-image-snapshot: ^4.3.0 jest-jasmine2: ^26.6.3 + jest-os-detection: ^1.3.1 jest-raw-loader: ^1.0.1 jest-serializer-html: ^7.0.0 jest-teamcity: ^1.9.0 @@ -28640,6 +28641,13 @@ fsevents@^1.2.7: languageName: node linkType: hard +"jest-os-detection@npm:^1.3.1": + version: 1.3.1 + resolution: "jest-os-detection@npm:1.3.1" + checksum: 0fd540b9518d443fe037bd74c3413869e23b167faa3c3f2653c652eb9b0f4acf5448df92344bdf67f4d0d6e994294513e3ba6b0507c245e5d9d31e83541ac621 + languageName: node + linkType: hard + "jest-pnp-resolver@npm:^1.2.1, jest-pnp-resolver@npm:^1.2.2": version: 1.2.2 resolution: "jest-pnp-resolver@npm:1.2.2"