-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Testing library command, heavily refactor (#18)
* Add testing command * Add copyTemplateDirectory utility * Fix issues, tests, refactor
- Loading branch information
Stephen Hanson
authored
Nov 10, 2023
1 parent
c1c2d97
commit 9cc2fb1
Showing
40 changed files
with
728 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,70 @@ | ||
import actualfs from 'fs-extra'; | ||
import { fs } from 'memfs'; | ||
import { vi } from 'vitest'; | ||
import path from 'path'; | ||
|
||
const DONT_MOCK_PATTERNS = ['templates/']; | ||
let DONT_MOCK_PATTERNS = ['templates/']; | ||
|
||
// Most of this CLI uses fs-extra for file-system operations | ||
// This mock mocks this module to instead use memfs, while also allowing | ||
// read operations to read from 'templates' so our tests do not need to | ||
// mock templates | ||
export default { | ||
// allow mocking templates with memfs (normally tests read these from regular FS) | ||
mockTemplates() { | ||
DONT_MOCK_PATTERNS = []; | ||
}, | ||
...fs.promises, | ||
// todo: build actual mock | ||
copySync: vi.fn(), | ||
exists(path) { | ||
return new Promise((resolve) => { | ||
fs.exists(path, (exists) => resolve(exists)); | ||
}); | ||
}, | ||
readFile: (...args) => { | ||
const path = args[0]; | ||
const useActual = DONT_MOCK_PATTERNS.some((pattern) => | ||
path.includes(pattern), | ||
); | ||
isDirectory(src) { | ||
return fs.statSync(src).isDirectory(src); | ||
}, | ||
// currently having to manually copy the sync methods over, there's prob a better way | ||
rmSync: fs.rmSync, | ||
readFileSync(file, options) { | ||
if (dontMock(file)) { | ||
return actualfs.readFileSync(file, options); | ||
} | ||
|
||
if (useActual) { | ||
return actualfs.readFile(...args); | ||
return fs.readFileSync(file, options); | ||
}, | ||
writeFileSync: fs.writeFileSync, | ||
existsSync: fs.existsSync, | ||
appendFileSync: fs.appendFileSync, | ||
readdirSync(path, options) { | ||
if (dontMock(path)) { | ||
return actualfs.readdirSync(path, options); | ||
} | ||
|
||
return fs.promises.readFile(...args); | ||
return fs.readdirSync(path, options); | ||
}, | ||
copySync(src, dest) { | ||
const sourceFS = dontMock(src) ? actualfs : fs; | ||
|
||
if (sourceFS.existsSync(src) && sourceFS.statSync(src).isDirectory(src)) { | ||
fs.mkdirSync(dest, { recursive: true }); | ||
sourceFS.readdirSync(src).forEach((childItemName) => { | ||
this.copySync( | ||
path.join(src, childItemName), | ||
path.join(dest, childItemName), | ||
); | ||
}); | ||
} else { | ||
fs.writeFileSync(dest, sourceFS.readFileSync(src, 'utf-8')); | ||
} | ||
}, | ||
readFile: (path, ...args) => { | ||
if (dontMock(path)) { | ||
return actualfs.readFile(path, ...args); | ||
} | ||
|
||
return fs.promises.readFile(path, ...args); | ||
}, | ||
}; | ||
|
||
function dontMock(src) { | ||
return DONT_MOCK_PATTERNS.some((pattern) => src.includes(pattern)); | ||
} |
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
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
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
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 { vol } from 'memfs'; | ||
import { expect, test, vi } from 'vitest'; | ||
import addDependency from '../../util/addDependency'; | ||
import addTestingLibrary from '../testingLibrary'; | ||
|
||
vi.mock('../../util/addDependency'); | ||
vi.mock('../../util/print', () => ({ | ||
default: vi.fn(), | ||
})); | ||
|
||
test('installs Testing Library', async () => { | ||
vol.fromJSON({ | ||
'package.json': JSON.stringify({ | ||
scripts: {}, | ||
dependencies: { | ||
expo: '1.0.0', | ||
}, | ||
devDependencies: {}, | ||
}), | ||
'yarn.lock': '', | ||
}); | ||
|
||
await addTestingLibrary(); | ||
|
||
expect(addDependency).toHaveBeenCalled(); | ||
}); |
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
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
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 |
---|---|---|
@@ -1,37 +1,28 @@ | ||
import chalk from 'chalk'; | ||
import * as eta from 'eta'; | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
import { PACKAGE_ROOT } from '../constants'; | ||
import addDependency from '../util/addDependency'; | ||
import getProjectDir from '../util/getProjectDir'; | ||
import addPackageJsonScripts from '../util/addPackageJsonScripts'; | ||
import copyTemplateDirectory from '../util/copyTemplateDirectory'; | ||
import isEslintConfigured from '../util/isEslintConfigured'; | ||
import isPackageInstalled from '../util/isPackageInstalled'; | ||
import print from '../util/print'; | ||
import writeFile from '../util/writeFile'; | ||
|
||
export default async function addEslint() { | ||
const projectDir = await getProjectDir(); | ||
|
||
if (await isEslintConfigured()) { | ||
print('eslint config already exists'); | ||
} else { | ||
const hasTypeScript = await isPackageInstalled('typescript'); | ||
|
||
const eslintConfigTemplate = await fs.readFile( | ||
path.join(PACKAGE_ROOT, 'templates/eslintrc.js.eta'), | ||
); | ||
await addDependency('eslint @thoughtbot/eslint-config', { dev: true }); | ||
|
||
const fileContents = eta.render(eslintConfigTemplate.toString(), { | ||
typescript: hasTypeScript, | ||
await copyTemplateDirectory({ | ||
templateDir: 'eslint', | ||
variables: { typescript: hasTypeScript }, | ||
}); | ||
|
||
await writeFile(path.join(projectDir, '.eslintrc.js'), fileContents, { | ||
format: true, | ||
await addPackageJsonScripts({ | ||
'lint:eslint': 'eslint --max-warnings=0 --ext js,jsx,ts,tsx .', | ||
}); | ||
|
||
await addDependency('@thoughtbot/eslint-config', { dev: true }); | ||
|
||
print(chalk.green('🎉 ESLint successfully configured')); | ||
} | ||
} |
Oops, something went wrong.