Skip to content

Commit

Permalink
Add more tests for sku lint and sku format output (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
askoufis committed Jun 6, 2023
1 parent 87a0cda commit 6987a7b
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 106 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@sku-fixtures/import-order",
"name": "@sku-fixtures/lint-format",
"private": true,
"devDependencies": {
"sku": "workspace:*"
Expand Down
File renamed without changes.
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

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

34 changes: 0 additions & 34 deletions tests/__snapshots__/import-order.test.js.snap

This file was deleted.

84 changes: 84 additions & 0 deletions tests/__snapshots__/lint-format.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`sku format errors are fixed: fixableLintError.ts 1`] = `
const foo = () => 'foo';
`;

exports[`sku format errors are fixed: importOrder1.ts 1`] = `
import './reset'; // side-effect imports should stay put
import path from 'path'; // built-in
import someModule from 'some-module'; // external
import utils from 'src/utils'; // internal
import distantParent from '../../../parent'; // parents
import Parent from '../parent'; // parents
import LocalComponent from './LocalComponent'; // sibling
import myself from '.'; // index
import styles from './styles.less'; // styles
import vanillaStyles from './vanillaStyles.css'; // styles
`;

exports[`sku format errors are fixed: importOrder2.ts 1`] = `
import c from '../c';
import a from './a';
import b from './b';
import aStyle from './a.css';
import bStyle from './b.css';
import cStyle from '../c.css';
`;

exports[`sku format errors are fixed: unformattedFile2.ts 1`] = `
import { something } from 'with-double-quotes';
const indented = 'indented';
const foo = [
'something "really" long',
'that has to be moved',
'to multiple lines',
"so we can 'test' trailing commas",
];
`;

exports[`sku lint lint errors are reported: typescriptFile.ts 1`] = `
stdout: Linting
Checking code with TypeScript compiler
src/typescriptFile.ts(1,7): error TS2322: Type 'string' is not assignable to type 'number'.
`;

exports[`sku lint lint errors are reported: unformattedFile1.js 1`] = `
stdout: Linting
Checking code with TypeScript compiler
Checking code with Prettier
Paths: **/*.{ts,cts,mts,tsx,js,cjs,mjs,jsx,md,less,css}
src/unformattedFile1.js
To fix this issue, run 'npx sku format'
`;

exports[`sku lint lint errors are reported: utils.test.ts 1`] = `
stdout: Linting
Checking code with TypeScript compiler
Checking code with Prettier
Paths: **/*.{ts,cts,mts,tsx,js,cjs,mjs,jsx,md,less,css}
Checking code with ESLint
Paths: .
1:1 error Unexpected console statement no-console
3:4 error Unexpected focused test jest/no-focused-tests
4:7 error 'foo' is never reassigned. Use 'const' instead prefer-const
✖ 3 problems (3 errors, 0 warnings)
1 error and 0 warnings potentially fixable with the \`--fix\` option.
`;
61 changes: 0 additions & 61 deletions tests/import-order.test.js

This file was deleted.

144 changes: 144 additions & 0 deletions tests/lint-format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
const path = require('path');
const fs = require('fs/promises');
const dedent = require('dedent');
const { runSkuScriptInDir } = require('@sku-private/test-utils');

const appDirectory = path.dirname(
require.resolve('@sku-fixtures/lint-format/sku.config.js'),
);
const srcDirectory = path.join(appDirectory, 'src');
const testFile = (fileName) => path.join(srcDirectory, fileName);

const filesToLint = {
'utils.test.ts': dedent/* ts */ `
console.log('foo');
it.only('should test something', () => {
let foo = true;
expect(foo).toBe(true);
});\n`,
'typescriptFile.ts': dedent/* ts */ `
const foo: number = 'a string';
`,
'unformattedFile1.js': dedent/* js */ `
import { something } from "with-double-quotes";
const indented = 'indented';
const foo = [
"something \"really\" long",
"that has to be moved",
'to multiple lines',
"so we can 'test' trailing commas"
];
`,
};

const filesToFormat = {
'importOrder1.ts': dedent/* ts */ `
import './reset'; // side-effect imports should stay put
import LocalComponent from './LocalComponent'; // sibling
import Parent from '../parent'; // parents
import someModule from 'some-module'; // external
import vanillaStyles from './vanillaStyles.css'; // styles
import distantParent from '../../../parent'; // parents
import myself from '.'; // index
import path from 'path'; // built-in
import styles from './styles.less'; // styles
import utils from 'src/utils'; // internal
`,
'importOrder2.ts': dedent/* ts */ `
import aStyle from './a.css';
import bStyle from './b.css';
import cStyle from '../c.css';
import b from './b';
import a from './a';
import c from '../c';
`,
'fixableLintError.ts': dedent/* ts */ `
const foo = () => {
return 'foo';
};
`,
'unformattedFile2.ts': dedent/* ts */ `
import { something } from "with-double-quotes";
const indented = 'indented';
const foo = [
"something \"really\" long",
"that has to be moved",
'to multiple lines',
"so we can 'test' trailing commas"
];
`,
};

beforeEach(async () => {
await fs.mkdir(srcDirectory, { recursive: true });
});

afterEach(async () => {
await fs.rm(srcDirectory, { recursive: true, force: true });
});

describe('sku lint', () => {
expect.addSnapshotSerializer({
serialize: (val) => {
const { stdout } = val;
// Remove some logs that contain file paths that are unique to the machine
const sanitizedStdout = stdout
.split('\n')
.filter((line) => !line.includes('sku/fixtures/lint-format'))
.join('\n');

return dedent`
stdout: ${sanitizedStdout}
`;
},
test: (val) => typeof val === 'object' && val.hasOwnProperty('stdout'),
});

test.each(Object.keys(filesToLint))(
'lint errors are reported: %s',
async (fileName) => {
const filePath = testFile(fileName);
await fs.writeFile(filePath, filesToLint[fileName]);

let result;

try {
await runSkuScriptInDir('lint', appDirectory);
} catch (err) {
result = { stderr: err.stderr, stdout: err.stdout };
}

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

describe('sku format', () => {
expect.addSnapshotSerializer({
serialize: (val) => val,
test: (val) => typeof val === 'string',
});

test.each(Object.keys(filesToFormat))(
'errors are fixed: %s',
async (fileName) => {
const filePath = testFile(fileName);
await fs.writeFile(filePath, filesToFormat[fileName]);

await runSkuScriptInDir('format', appDirectory);

const result = await fs.readFile(filePath, { encoding: 'utf-8' });

expect(result).toMatchSnapshot();
},
);
});
2 changes: 1 addition & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@sku-fixtures/configure": "workspace:*",
"@sku-fixtures/braid-design-system": "workspace:*",
"@sku-fixtures/custom-src-paths": "workspace:*",
"@sku-fixtures/import-order": "workspace:*",
"@sku-fixtures/lint-format": "workspace:*",
"@sku-fixtures/library-build": "workspace:*",
"@sku-fixtures/library-file": "workspace:*",
"@sku-fixtures/multiple-routes": "workspace:*",
Expand Down

0 comments on commit 6987a7b

Please sign in to comment.