diff --git a/fixtures/import-order/.eslintignore b/fixtures/lint-format/.eslintignore similarity index 100% rename from fixtures/import-order/.eslintignore rename to fixtures/lint-format/.eslintignore diff --git a/fixtures/import-order/.gitignore b/fixtures/lint-format/.gitignore similarity index 100% rename from fixtures/import-order/.gitignore rename to fixtures/lint-format/.gitignore diff --git a/fixtures/import-order/.prettierignore b/fixtures/lint-format/.prettierignore similarity index 100% rename from fixtures/import-order/.prettierignore rename to fixtures/lint-format/.prettierignore diff --git a/fixtures/import-order/package.json b/fixtures/lint-format/package.json similarity index 65% rename from fixtures/import-order/package.json rename to fixtures/lint-format/package.json index 845d248d2..9e8b53732 100644 --- a/fixtures/import-order/package.json +++ b/fixtures/lint-format/package.json @@ -1,5 +1,5 @@ { - "name": "@sku-fixtures/import-order", + "name": "@sku-fixtures/lint-format", "private": true, "devDependencies": { "sku": "workspace:*" diff --git a/fixtures/import-order/sku.config.js b/fixtures/lint-format/sku.config.js similarity index 100% rename from fixtures/import-order/sku.config.js rename to fixtures/lint-format/sku.config.js diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 412767c39..02c40d9df 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -159,12 +159,6 @@ importers: specifier: workspace:* version: link:../../packages/sku - fixtures/import-order: - devDependencies: - sku: - specifier: workspace:* - version: link:../../packages/sku - fixtures/library-build: devDependencies: dedent: @@ -183,6 +177,12 @@ importers: specifier: workspace:* version: link:../../packages/sku + fixtures/lint-format: + devDependencies: + sku: + specifier: workspace:* + version: link:../../packages/sku + fixtures/multiple-routes: dependencies: react: @@ -790,15 +790,15 @@ importers: '@sku-fixtures/custom-src-paths': specifier: workspace:* version: link:../fixtures/custom-src-paths - '@sku-fixtures/import-order': - specifier: workspace:* - version: link:../fixtures/import-order '@sku-fixtures/library-build': specifier: workspace:* version: link:../fixtures/library-build '@sku-fixtures/library-file': specifier: workspace:* version: link:../fixtures/library-file + '@sku-fixtures/lint-format': + specifier: workspace:* + version: link:../fixtures/lint-format '@sku-fixtures/multiple-routes': specifier: workspace:* version: link:../fixtures/multiple-routes diff --git a/tests/__snapshots__/import-order.test.js.snap b/tests/__snapshots__/import-order.test.js.snap deleted file mode 100644 index 8c2c9b581..000000000 --- a/tests/__snapshots__/import-order.test.js.snap +++ /dev/null @@ -1,34 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`import order imports are ordered: index.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[`import order imports are ordered: vanilla.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'; - -`; diff --git a/tests/__snapshots__/lint-format.test.js.snap b/tests/__snapshots__/lint-format.test.js.snap new file mode 100644 index 000000000..ad1d4fbd4 --- /dev/null +++ b/tests/__snapshots__/lint-format.test.js.snap @@ -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. +`; diff --git a/tests/import-order.test.js b/tests/import-order.test.js deleted file mode 100644 index 35ecfd0f1..000000000 --- a/tests/import-order.test.js +++ /dev/null @@ -1,61 +0,0 @@ -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/import-order/sku.config.js'), -); -const srcDirectory = path.join(appDirectory, 'src'); -const testFile = (fileName) => path.join(srcDirectory, fileName); - -expect.addSnapshotSerializer({ - serialize: (val) => val, - test: (val) => typeof val === 'string', -}); - -const files = { - 'index.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 - `, - 'vanilla.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'; - `, -}; - -describe('import order', () => { - beforeAll(async () => { - await fs.mkdir(srcDirectory, { recursive: true }); - }); - - afterAll(async () => { - await fs.rm(srcDirectory, { recursive: true, force: true }); - }); - - test.each(Object.keys(files))('imports are ordered: %s', async (fileName) => { - const filePath = testFile(fileName); - await fs.writeFile(filePath, files[fileName]); - - await runSkuScriptInDir('format', appDirectory); - - const result = await fs.readFile(filePath, { encoding: 'utf-8' }); - - expect(result).toMatchSnapshot(); - }); -}); diff --git a/tests/lint-format.test.js b/tests/lint-format.test.js new file mode 100644 index 000000000..0a0c93208 --- /dev/null +++ b/tests/lint-format.test.js @@ -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(); + }, + ); +}); diff --git a/tests/package.json b/tests/package.json index e002af14c..77dfb85b4 100644 --- a/tests/package.json +++ b/tests/package.json @@ -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:*",