Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
fix: Correct unit tests failing on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
stephan-noel committed Aug 2, 2020
1 parent fb29f40 commit c63de5d
Show file tree
Hide file tree
Showing 19 changed files with 310 additions and 43 deletions.
5 changes: 3 additions & 2 deletions core/cli/__tests__/cli.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Path from 'path';
import execa = require('execa');
import stripAnsi from 'strip-ansi';
import { normalizeContainedPath } from '@garment/fixture-helper';

const cwd = Path.join(__dirname + '/fixtures/basic');
const garmentPath = Path.relative(cwd, 'core/cli/lib/cli.js');
Expand Down Expand Up @@ -48,12 +49,12 @@ test('cli should fail if given incorrect number of commands', async () => {
const { stderr } = await execaSafe('node', [garmentPath, noCacheArg], {
cwd: cwd
});
expect(stderr).toMatchSnapshot();
expect(normalizeContainedPath(stderr)).toMatchSnapshot();
});

test('cli should fail if given unexisting command', async () => {
const { stderr } = await execaSafe('node', [garmentPath, 'dep', noCacheArg], {
cwd: cwd
});
expect(stderr).toMatchSnapshot();
expect(normalizeContainedPath(stderr)).toMatchSnapshot();
});
3 changes: 2 additions & 1 deletion core/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@types/update-notifier": "^2.5.0",
"@types/yargs": "13.0.3",
"execa": "^4.0.0",
"strip-ansi": "^5.2.0"
"strip-ansi": "^5.2.0",
"@garment/fixture-helper": "^0.13.8"
}
}
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ module.exports = {
'ts-jest': {
tsConfig: 'tsconfig.test.json'
}
}
},
resolver: './utils/fixture-helper/lib/resolver'
};
7 changes: 6 additions & 1 deletion plugins/runner-bin/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parseOptions, resolveBin } from '../src/utils';
import * as Path from 'path';
import os = require('os');

describe('parseOptions function ', () => {
const expectedResult = ['prettier', '-o', '--output-dir', 'index.js'];
Expand Down Expand Up @@ -60,7 +61,11 @@ describe('resolveBin function ', () => {
);

test('if gets bin name returns as resolved bin', async () => {
expect(await resolveBin('prettier')).toBe(expectedResult);
const expectedPlatformResult = Path.format({
name: expectedResult,
ext: os.platform() === 'win32' ? '.CMD' : ''
});
expect(await resolveBin('prettier')).toBe(expectedPlatformResult);
});

test('if gets bin path returns as fullPath', async () => {
Expand Down
18 changes: 5 additions & 13 deletions plugins/runner-jest/__tests__/getConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { createBatchContext, initFixtureHelper } from '@garment/fixture-helper';
import {
createBatchContext,
initFixtureHelper,
replaceTestPath
} from '@garment/fixture-helper';
import { getConfig, JestRunnerOptions } from '../src';

const { initFixture, clean } = initFixtureHelper(module, {
Expand Down Expand Up @@ -37,15 +41,3 @@ test('Handles multiple projects sharing the same config and one having different

expect(replaceTestPath(jestConfig, cwd)).toMatchSnapshot();
});

function replaceTestPath<T>(
obj: T,
testPath: string,
replaceWith = '/test_path'
): T {
return JSON.parse(
JSON.stringify(obj, (_, value) =>
typeof value === 'string' ? value.replace(testPath, replaceWith) : value
)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Object {
"collectedOutput": Array [
Object {
"cacheKeys": Array [
"8de170ce8b6d1c1aeb78d959f4faa0cf",
"mockHash",
"
@import './test.css';
.Slideshow {
Expand Down Expand Up @@ -60,7 +60,7 @@ Object {
"collectedOutput": Array [
Object {
"cacheKeys": Array [
"e1c061eef28e0cce2b1ff22fb7f6e12c",
"mockHash",
"
:root {
--color: red;
Expand Down Expand Up @@ -111,7 +111,7 @@ Object {
"collectedOutput": Array [
Object {
"cacheKeys": Array [
"8de170ce8b6d1c1aeb78d959f4faa0cf",
"mockHash",
":fullscreen {}",
"index.css",
],
Expand Down
56 changes: 55 additions & 1 deletion plugins/runner-postcss/__tests__/default.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { executeRunner2, initFixtureHelper } from '@garment/fixture-helper';
import {
executeRunner2,
initFixtureHelper,
ExecuteRunner2Result
} from '@garment/fixture-helper';
import * as Path from 'path';
import handler from '../src';
import os = require('os');

const { initFixture, clean } = initFixtureHelper(module, {
tempDir: __dirname + 'tmp__'
Expand All @@ -21,7 +26,15 @@ test('Uses postcss.config.js from workspace root', async () => {
files: file => [file.text('index.css', `:fullscreen {}`)]
});

const [hash] = extractHashes(result, 'mockHash');

expect(result).toMatchSnapshot();

if (os.platform() === 'win32') {
expect(hash).toBe('b2fa5e79e89879036728c19a038eafac');
} else {
expect(hash).toBe('8de170ce8b6d1c1aeb78d959f4faa0cf');
}
});

test('Uses postcss.config.js from project', async () => {
Expand All @@ -47,7 +60,15 @@ test('Uses postcss.config.js from project', async () => {
}
);

const [hash] = extractHashes(result, 'mockHash');

expect(result).toMatchSnapshot();

if (os.platform() === 'win32') {
expect(hash).toBe('85e0de080544f7c03f6fa022840ccdaa');
} else {
expect(hash).toBe('e1c061eef28e0cce2b1ff22fb7f6e12c');
}
});

test('Resolves postcss @import', async () => {
Expand All @@ -74,5 +95,38 @@ test('Resolves postcss @import', async () => {
]
});

const [hash] = extractHashes(result, 'mockHash');

expect(result).toMatchSnapshot();

if (os.platform() === 'win32') {
expect(hash).toBe('b2fa5e79e89879036728c19a038eafac');
} else {
expect(hash).toBe('8de170ce8b6d1c1aeb78d959f4faa0cf');
}
});

function extractHashes(
result: ExecuteRunner2Result,
replacement: string
): string[] {
const { collectedOutput } = result;
const md5Regex = /^[a-f0-9]{32}$/i;

const originalHashes: string[] = [];

collectedOutput.forEach((output: any) => {
if (output.hasOwnProperty('cacheKeys')) {
output.cacheKeys = output.cacheKeys.map((cacheKey: string) => {
if (md5Regex.test(cacheKey)) {
originalHashes.push(cacheKey);
return replacement;
} else {
return cacheKey;
}
});
}
});

return originalHashes;
}
52 changes: 52 additions & 0 deletions plugins/runner-ts/__tests__/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,58 @@ Object {
}
`;

exports[`Transpiles TypeScript when configFile contains npm module in extends field 1`] = `
Object {
"collectedOutput": Array [
Object {
"cacheKeys": Array [
"3.8.3",
"{\\"declaration\\":true,\\"sourceMap\\":true,\\"noEmitOnError\\":true,\\"skipLibCheck\\":true,\\"target\\":\\"es5\\",\\"module\\":\\"esnext\\",\\"moduleResolution\\":\\"node\\"}",
"export const greet = (str: string) => \`Hello, \${str}\`",
],
"dependencies": Array [],
"files": Array [
Object {
"data": "{\\"version\\":3,\\"file\\":\\"index.js\\",\\"sourceRoot\\":\\"\\",\\"sources\\":[\\"index.ts\\"],\\"names\\":[],\\"mappings\\":\\"AAAA,MAAM,CAAC,IAAM,KAAK,GAAG,UAAC,GAAW,IAAK,OAAA,YAAU,GAAK,EAAf,CAAe,CAAA\\"}",
"path": "nested/index.js.map",
"type": "text",
},
Object {
"data": "export var greet = function (str) { return \\"Hello, \\" + str; };
//# sourceMappingURL=index.js.map",
"path": "nested/index.js",
"type": "text",
},
Object {
"data": "export declare const greet: (str: string) => string;
",
"path": "nested/index.d.ts",
"type": "text",
},
],
"logs": Array [],
},
],
"dependsOnFile": Array [
"/test_path/tsconfig.json",
"/test_path/node_modules/@my-scope/tsconfig/tsconfig.base.json",
"/test_path/node_modules/@my-scope/tsconfig/tsconfig.common.json",
],
"isLongRunning": false,
"logs": Array [
Object {
"args": Array [
"[TS] Processing",
"index.ts",
],
"level": "debug",
},
],
"onDestroyHandler": undefined,
"shouldWatchDependencies": false,
}
`;

exports[`Transpiles TypeScript with relative imports including not provided as input 1`] = `
Object {
"collectedOutput": Array [
Expand Down
29 changes: 28 additions & 1 deletion plugins/runner-ts/__tests__/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as Path from 'path';
import handler from '../src';

const { initFixture, clean } = initFixtureHelper(module, {
tempDir: __dirname + 'tmp__'
tempDir: Path.join(__dirname, 'tmp__')
});

afterAll(clean);
Expand Down Expand Up @@ -85,3 +85,30 @@ test('Transpiles TypeScript', async () => {

expect(result).toMatchSnapshot();
});

test('Transpiles TypeScript when configFile contains npm module in extends field', async () => {
const cwd = await initFixture('config-extends-npm-module', { staticId: 0 });

const src = (p = '') => Path.join(cwd, 'test-project', 'src', p);

const result = await executeRunner2(
handler,
{ configFile: 'tsconfig.json' },
{
projectRelativePath: 'test-project',
cwd,
files: file => [
file.text(
'index.ts',
'export const greet = (str: string) => `Hello, ${str}`',
{
absolutePath: src('nested/index.ts'),
baseDir: src()
}
)
]
}
);

expect(result).toMatchSnapshot();
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "@my-scope/tsconfig/tsconfig.base.json",
"compilerOptions": {
"target": "es5"
}
}
4 changes: 4 additions & 0 deletions plugins/runner-ts/__tests__/module-resolution.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"@my-scope/tsconfig/tsconfig.base.json": "tmp__/default-config-extends-npm-module-0/node_modules/@my-scope/tsconfig/tsconfig.base.json",
"@my-scope/tsconfig/tsconfig.common.json": "tmp__/default-config-extends-npm-module-0/node_modules/@my-scope/tsconfig/tsconfig.common.json"
}
Loading

0 comments on commit c63de5d

Please sign in to comment.