Skip to content

Commit

Permalink
test(windows): fix tests when run on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed May 29, 2022
1 parent c5e87c2 commit 3371362
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 42 deletions.
1 change: 0 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ module.exports = {
},
moduleFileExtensions: ['ts', 'js'],
setupFiles: ['<rootDir>/test/jest.setup.ts'],
testMatch: ['<rootDir>/src/**/*.spec.(ts|js)'],
transform: {
'^.+\\.ts$': 'ts-jest',
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@
"format:source": "prettier --write .",
"lint": "eslint --ext .ts .",
"prepack": "yarn build",
"test": "jest"
"test": "jest src"
}
}
25 changes: 13 additions & 12 deletions src/bin-format/format.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'expect-more-jest';
import { normalize } from 'path';
import { createWrapper, toJson } from '../../test/mock';
import { mockDisk } from '../../test/mock-disk';
import { DEFAULT_CONFIG } from '../constants';
Expand All @@ -19,13 +20,13 @@ describe('format', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
format(input, disk);
expect(disk.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('/some/package.json'),
expect.stringContaining(normalize('/some/package.json')),
json,
);
expect(before).toEqual(after);
expect(log).toHaveBeenCalledWith(
expect.stringMatching(//),
expect.stringContaining('/some/package.json'),
expect.stringContaining(normalize('some/package.json')),
);
});
it('sorts object properties alphabetically by key', () => {
Expand All @@ -41,13 +42,13 @@ describe('format', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
format(input, disk);
expect(disk.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('/some/package.json'),
expect.stringContaining(normalize('/some/package.json')),
json,
);
expect(before).toEqual(after);
expect(log).toHaveBeenCalledWith(
expect.stringMatching(//),
expect.stringContaining('/some/package.json'),
expect.stringContaining(normalize('some/package.json')),
);
});
it('sorts named properties first, then the rest alphabetically', () => {
Expand All @@ -63,13 +64,13 @@ describe('format', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
format(input, disk);
expect(disk.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('/some/package.json'),
expect.stringContaining(normalize('/some/package.json')),
json,
);
expect(before).toEqual(after);
expect(log).toHaveBeenCalledWith(
expect.stringMatching(//),
expect.stringContaining('/some/package.json'),
expect.stringContaining(normalize('some/package.json')),
);
});
it('uses shorthand format for "bugs"', () => {
Expand All @@ -84,13 +85,13 @@ describe('format', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
format(input, disk);
expect(disk.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('/some/package.json'),
expect.stringContaining(normalize('/some/package.json')),
json,
);
expect(before).toEqual(after);
expect(log).toHaveBeenCalledWith(
expect.stringMatching(//),
expect.stringContaining('/some/package.json'),
expect.stringContaining(normalize('some/package.json')),
);
});
it('uses shorthand format for "repository"', () => {
Expand All @@ -107,13 +108,13 @@ describe('format', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
format(input, disk);
expect(disk.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('/some/package.json'),
expect.stringContaining(normalize('/some/package.json')),
json,
);
expect(before).toEqual(after);
expect(log).toHaveBeenCalledWith(
expect.stringMatching(//),
expect.stringContaining('/some/package.json'),
expect.stringContaining(normalize('some/package.json')),
);
});
it('uses github shorthand format for "repository"', () => {
Expand All @@ -130,13 +131,13 @@ describe('format', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
format(input, disk);
expect(disk.writeFileSync).toHaveBeenCalledWith(
expect.stringContaining('/some/package.json'),
expect.stringContaining(normalize('/some/package.json')),
json,
);
expect(before).toEqual(after);
expect(log).toHaveBeenCalledWith(
expect.stringMatching(//),
expect.stringContaining('/some/package.json'),
expect.stringContaining(normalize('some/package.json')),
);
});
});
7 changes: 1 addition & 6 deletions src/lib/get-input/get-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'expect-more-jest';
import { join } from 'path';
import { getInput } from '.';
import { mockDisk } from '../../../test/mock-disk';
import { DEFAULT_CONFIG } from '../../constants';
import { CWD, DEFAULT_CONFIG } from '../../constants';

describe('getInput', () => {
describe('dependencyTypes', () => {
Expand Down Expand Up @@ -133,7 +133,6 @@ describe('getInput', () => {
describe('when --source cli options are given', () => {
describe('for a single package.json file', () => {
it('reads that file only', () => {
const CWD = process.cwd();
const filePath = join(CWD, 'package.json');
const contents = { name: 'foo' };
const json = '{"name":"foo"}';
Expand Down Expand Up @@ -171,7 +170,6 @@ describe('getInput', () => {
describe('when yarn workspaces are defined', () => {
describe('as an array', () => {
it('resolves yarn workspace packages', () => {
const CWD = process.cwd();
const filePath = join(CWD, 'package.json');
const contents = { workspaces: ['./as-array/*'] };
const json = JSON.stringify(contents);
Expand All @@ -187,7 +185,6 @@ describe('getInput', () => {
});
describe('as an object', () => {
it('resolves yarn workspace packages', () => {
const CWD = process.cwd();
const filePath = join(CWD, 'package.json');
const contents = { workspaces: { packages: ['./as-object/*'] } };
const json = JSON.stringify(contents);
Expand All @@ -205,7 +202,6 @@ describe('getInput', () => {
describe('when yarn workspaces are not defined', () => {
describe('when lerna.json is defined', () => {
it('resolves lerna packages', () => {
const CWD = process.cwd();
const filePath = join(CWD, 'package.json');
const contents = { name: 'foo' };
const json = JSON.stringify(contents);
Expand All @@ -226,7 +222,6 @@ describe('getInput', () => {
describe('when lerna.json is not defined', () => {
describe('when pnpm workspaces are defined', () => {
it('resolves pnpm packages', () => {
const CWD = process.cwd();
const filePath = join(CWD, 'package.json');
const disk = mockDisk();
disk.globSync.mockReturnValue([filePath]);
Expand Down
6 changes: 4 additions & 2 deletions test/mock.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { EOL } from 'os';
import { join, normalize } from 'path';
import { CWD } from '../src/constants';
import type { Source, SourceWrapper } from '../src/lib/get-input/get-wrappers';

export function createWrapper(contents: Source): SourceWrapper {
return withJson({ contents, filePath: '/some/package.json' });
return withJson({ contents, filePath: join(CWD, 'some/package.json') });
}

export function toJson(contents: SourceWrapper['contents']): string {
Expand Down Expand Up @@ -43,7 +45,7 @@ export const mockPackage = (
: {}),
...(otherProps ? otherProps : {}),
},
filePath: `/${dirName}/package.json`,
filePath: join(CWD, `${dirName}/package.json`),
});
};

Expand Down
63 changes: 43 additions & 20 deletions test/scenarios/create-scenario.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import minimatch from 'minimatch';
import { join } from 'path';
import { join, normalize } from 'path';
import type { SyncpackConfig } from '../../src/constants';
import { CWD } from '../../src/constants';
import type { SourceWrapper } from '../../src/lib/get-input/get-wrappers';
import type { MockDisk } from '../mock-disk';
import { mockDisk } from '../mock-disk';
Expand All @@ -10,6 +11,7 @@ interface MockedFile {
after: SourceWrapper;
before: SourceWrapper;
diskWriteWhenChanged: [string, string];
id: string;
logEntryWhenChanged: [any, any];
logEntryWhenUnchanged: [any, any];
relativePath: string;
Expand All @@ -33,43 +35,64 @@ export function createScenario(
const disk = mockDisk();
const log = jest.spyOn(console, 'log').mockImplementation(() => undefined);
// resolve all paths
const mockedFiles: MockedFile[] = fileMocks.map((file) => ({
absolutePath: join(process.cwd(), file.path),
after: file.after,
before: file.before,
diskWriteWhenChanged: [expect.stringContaining(file.path), file.after.json],
logEntryWhenChanged: [
expect.stringMatching(//),
expect.stringContaining(file.path),
],
logEntryWhenUnchanged: [
expect.stringMatching(/-/),
expect.stringContaining(file.path),
],
relativePath: file.path,
}));
const mockedFiles: MockedFile[] = fileMocks.map((file) => {
const absolutePath = join(CWD, file.path);
const relativePath = normalize(file.path);
return {
absolutePath,
after: {
...file.after,
filePath: absolutePath,
},
before: {
...file.before,
filePath: absolutePath,
},
diskWriteWhenChanged: [
expect.stringContaining(relativePath),
file.after.json,
],
id: file.path,
logEntryWhenChanged: [
expect.stringMatching(//),
expect.stringContaining(relativePath),
],
logEntryWhenUnchanged: [
expect.stringMatching(/-/),
expect.stringContaining(relativePath),
],
relativePath,
};
});
// mock file system
disk.readFileSync.mockImplementation((filePath): string | undefined => {
return mockedFiles.find((file) => {
return filePath === file.absolutePath;
return normalize(filePath) === normalize(file.absolutePath);
})?.before?.json;
});
// mock globs
disk.globSync.mockImplementation((pattern): string[] => {
return mockedFiles
.filter((file) => {
return minimatch(file.absolutePath, join(process.cwd(), pattern));
return minimatch(
normalize(file.absolutePath),
toPosix(join(CWD, pattern)),
);
})
.map((file) => file.absolutePath);
.map((file) => normalize(file.absolutePath));
});
// return API
return {
config,
disk,
log,
files: mockedFiles.reduce((memo, file) => {
memo[file.relativePath] = file;
memo[file.id] = file;
return memo;
}, {} as Record<string, MockedFile>),
};
}

function toPosix(value: string): string {
return value.replace('C:', '').replace(/\\/g, '/');
}

0 comments on commit 3371362

Please sign in to comment.