generated from mizdra/npm-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from mizdra/add-examples
Add examples
- Loading branch information
Showing
5 changed files
with
272 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import dedent from 'dedent'; | ||
import { ESLint } from 'eslint'; | ||
import { expect, test } from 'vitest'; | ||
import { createIFFByRandomRootDir } from './util/create-iff-by-random-root-dir.js'; | ||
|
||
test('eslint reports lint errors', async () => { | ||
const iff = await createIFFByRandomRootDir({ | ||
'.eslintrc.cjs': `module.exports = { root: true, rules: { semi: 'error' } };`, | ||
'src': { | ||
'semi.js': dedent` | ||
var withSemicolon = 1; | ||
var withoutSemicolon = 2 | ||
`, | ||
}, | ||
}); | ||
|
||
const eslint = new ESLint({ cwd: iff.rootDir, useEslintrc: true }); | ||
const results = await eslint.lintFiles([iff.paths['src/semi.js']]); | ||
const formatter = await eslint.loadFormatter('unix'); | ||
const resultText = formatter.format(results); | ||
expect(resultText).toStrictEqual(dedent` | ||
${iff.paths['src/semi.js']}:2:25: Missing semicolon. [Error/semi] | ||
1 problem | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { readFile } from 'node:fs/promises'; | ||
import dedent from 'dedent'; | ||
import { ESLint } from 'eslint'; | ||
import { describe, expect, it } from 'vitest'; | ||
import { createIFFByRandomRootDir } from './util/create-iff-by-random-root-dir.js'; | ||
|
||
describe('eslint', async () => { | ||
// Share `.eslintrc.cjs` between test cases. | ||
const baseIFF = await createIFFByRandomRootDir({ | ||
'.eslintrc.cjs': `module.exports = { root: true, rules: { semi: 'error' } };`, | ||
}); | ||
|
||
it('reports lint errors', async () => { | ||
// The `fork` allows you to change the `rootDir` of fixtures while inheriting the fixtures from `baseIFF`. | ||
const iff = await baseIFF.fork({ | ||
src: { | ||
'semi.js': dedent` | ||
var withSemicolon = 1; | ||
var withoutSemicolon = 2 | ||
`, | ||
}, | ||
}); | ||
const eslint = new ESLint({ cwd: iff.rootDir, useEslintrc: true }); | ||
const results = await eslint.lintFiles([iff.paths['src/semi.js']]); | ||
const formatter = await eslint.loadFormatter('unix'); | ||
const resultText = formatter.format(results); | ||
expect(resultText).toStrictEqual(dedent` | ||
${iff.paths['src/semi.js']}:2:25: Missing semicolon. [Error/semi] | ||
1 problem | ||
`); | ||
}); | ||
it('fix lint errors', async () => { | ||
const iff = await baseIFF.fork({ | ||
src: { | ||
'semi.js': dedent` | ||
var withoutSemicolon = 2 | ||
`, | ||
}, | ||
}); | ||
const eslint = new ESLint({ cwd: iff.rootDir, useEslintrc: true, fix: true }); | ||
const results = await eslint.lintFiles([iff.paths['src/semi.js']]); | ||
|
||
expect(await readFile(iff.paths['src/semi.js'], 'utf8')).toMatchInlineSnapshot('"var withoutSemicolon = 2"'); | ||
await ESLint.outputFixes(results); | ||
expect(await readFile(iff.paths['src/semi.js'], 'utf8')).toMatchInlineSnapshot('"var withoutSemicolon = 2;"'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { randomUUID } from 'node:crypto'; | ||
import { tmpdir } from 'node:os'; | ||
import { join } from 'node:path'; | ||
import { createIFF, Directory } from '../../src/index.js'; | ||
/** | ||
* The root directory for fixtures. | ||
* | ||
* NOTE: To avoid bloating `fixtureDir`, it is recommended to delete `fixtureDir` at the beginning of the test. | ||
* ```ts | ||
* // vitest.setup.ts | ||
* import { rm } from 'node:fs/promises'; | ||
* await rm(fixtureDir, { recursive: true, force: true }); | ||
* ``` | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const fixtureDir = join(tmpdir(), 'inline-fs-fixtures', process.env['VITEST_POOL_ID']!); | ||
|
||
export async function createIFFByRandomRootDir<const T extends Directory>(directory: T) { | ||
const getRandomRootDir = () => join(fixtureDir, randomUUID()); | ||
const iff = await createIFF(directory, { rootDir: getRandomRootDir() }); | ||
return { | ||
...iff, | ||
fork: async function forkImpl<const U extends Directory>(additionalDirectory: U) { | ||
const forkedIff = await iff.fork(additionalDirectory, { rootDir: getRandomRootDir() }); | ||
return { ...forkedIff, fork: forkImpl }; | ||
}, | ||
}; | ||
} |