Skip to content

Commit

Permalink
[wip] Fix unit tests on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS committed Nov 24, 2021
1 parent 55299a7 commit 1faf93d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path';
import { getPreviewFile, getMainFile } from './configure';

// eslint-disable-next-line global-require, jest/no-mocks-import
Expand All @@ -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', () => {
Expand All @@ -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', () => {
Expand Down
9 changes: 5 additions & 4 deletions app/react/src/server/cra-config.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from 'fs';
import path from 'path';
import { getReactScriptsPath } from './cra-config';

jest.mock('fs', () => ({
Expand All @@ -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', () => {
Expand All @@ -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')
);
});
});
Expand All @@ -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')
);
});
});
Expand Down Expand Up @@ -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')
);
});
});
Expand Down
11 changes: 8 additions & 3 deletions lib/core-server/src/utils/__tests__/server-statics.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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'),
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 1faf93d

Please sign in to comment.