-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
461 additions
and
14 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
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,12 @@ | ||
{ | ||
"extends": ["../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"parserOptions": { | ||
"project": ["testing/test-setup/tsconfig.*?.json"] | ||
} | ||
} | ||
] | ||
} |
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,16 @@ | ||
# test-setup | ||
|
||
This library contains test setup. | ||
|
||
## Mock setup | ||
|
||
In this library you can find all files that can be used in `setupFiles` property of `vitest.config.(unit|integration|e2e).ts` files. Currently include: | ||
|
||
- [console](./src/lib/console.mock.ts) mocking | ||
- [file system](./src/lib/fs.mock.ts) mocking | ||
- [reset](./src/lib/reset.mock.ts) mocking | ||
|
||
Additionally, you may find helper functions for: | ||
|
||
- setting up and tearing down a [testing folder](./src/lib/test-folder.setup.ts) | ||
- [resetting](./src/lib/reset.mocks.ts) mocks |
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,16 @@ | ||
{ | ||
"name": "test-setup", | ||
"$schema": "../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "testing/test-setup/src", | ||
"projectType": "library", | ||
"targets": { | ||
"lint": { | ||
"executor": "@nx/linter:eslint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": ["testing/test-setup/**/*.ts"] | ||
} | ||
} | ||
}, | ||
"tags": ["scope:shared", "type:testing"] | ||
} |
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,29 @@ | ||
import { type MockInstance, afterEach, beforeEach, vi } from 'vitest'; | ||
|
||
let consoleInfoSpy: MockInstance<unknown[], void> | undefined; | ||
let consoleWarnSpy: MockInstance<unknown[], void> | undefined; | ||
let consoleErrorSpy: MockInstance<unknown[], void> | undefined; | ||
|
||
beforeEach(() => { | ||
// In multi-progress-bars, console methods are overriden | ||
if (console.info != null) { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
consoleInfoSpy = vi.spyOn(console, 'info').mockImplementation(() => {}); | ||
} | ||
|
||
if (console.warn != null) { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); | ||
} | ||
|
||
if (console.error != null) { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); | ||
} | ||
}); | ||
|
||
afterEach(() => { | ||
consoleInfoSpy?.mockRestore(); | ||
consoleWarnSpy?.mockRestore(); | ||
consoleErrorSpy?.mockRestore(); | ||
}); |
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,21 @@ | ||
import { type MockInstance, afterEach, beforeEach, vi } from 'vitest'; | ||
import { MEMFS_VOLUME } from '@org/test-utils'; | ||
|
||
vi.mock('fs', async () => { | ||
const memfs: typeof import('memfs') = await vi.importActual('memfs'); | ||
return memfs.fs; | ||
}); | ||
vi.mock('fs/promises', async () => { | ||
const memfs: typeof import('memfs') = await vi.importActual('memfs'); | ||
return memfs.fs.promises; | ||
}); | ||
|
||
let cwdSpy: MockInstance<[], string>; | ||
|
||
beforeEach(() => { | ||
cwdSpy = vi.spyOn(process, 'cwd').mockReturnValue(MEMFS_VOLUME); | ||
}); | ||
|
||
afterEach(() => { | ||
cwdSpy.mockRestore(); | ||
}); |
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,7 @@ | ||
import { vol } from 'memfs'; | ||
import { beforeEach, vi } from 'vitest'; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
vol.reset(); | ||
}); |
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,19 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"module": "ESNext", | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"noImplicitOverride": true, | ||
"noPropertyAccessFromIndexSignature": true, | ||
"noImplicitReturns": true, | ||
"noFallthroughCasesInSwitch": true | ||
}, | ||
"files": [], | ||
"include": [], | ||
"references": [ | ||
{ | ||
"path": "./tsconfig.lib.json" | ||
} | ||
] | ||
} |
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,10 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "../dist/out-tsc", | ||
"declaration": true, | ||
"types": ["node"] | ||
}, | ||
"include": ["src/**/*.ts"], | ||
"exclude": [] | ||
} |
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,36 @@ | ||
const interval = parseInt(process.argv[2] || 100); | ||
let runs = parseInt(process.argv[3] || 4); | ||
let throwError = process.argv[4] === '1'; | ||
|
||
/** | ||
* Custom runner implementation that simulates asynchronous situations. | ||
* It logs progress to the console with a configurable interval and defaults to 100ms. | ||
* The number of runs is also configurable and defaults to 4. | ||
* We can decide if the process should error or complete. By default, it completes. | ||
* | ||
* @arg interval: number - delay between updates in ms; defaults to 100 | ||
* @arg runs: number - number of updates; defaults to 4 | ||
* @arg throwError: '1' | '0' - if the process completes or throws; defaults to '0' | ||
**/ | ||
(async () => { | ||
console.info( | ||
`process:start with interval: ${interval}, runs: ${runs}, throwError: ${throwError}` | ||
); | ||
await new Promise((resolve) => { | ||
const id = setInterval(() => { | ||
if (runs === 0) { | ||
clearInterval(id); | ||
if (throwError) { | ||
throw new Error('dummy-error'); | ||
} else { | ||
resolve('result'); | ||
} | ||
} else { | ||
runs--; | ||
console.info('process:update'); | ||
} | ||
}, interval); | ||
}); | ||
|
||
console.info('process:complete'); | ||
})(); |
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,3 +1,6 @@ | ||
export * from './lib/execute-process'; | ||
export * from './lib/terminal-command'; | ||
export * from './lib/setup'; | ||
export * from './lib/execute-process-helper.mock'; | ||
export * from './lib/test-folder.setup'; | ||
export * from './lib/constants'; |
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 @@ | ||
export const MEMFS_VOLUME = '/test'; |
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,22 @@ | ||
import { join } from 'path'; | ||
|
||
const asyncProcessPath = join(__dirname, '../../mock/execute-process.mock.mjs'); | ||
|
||
/** | ||
* Helps to get an async process runner config for testing. | ||
* | ||
* @param cfg can contain up to three properties for the async process runner | ||
*/ | ||
export function getAsyncProcessRunnerConfig(cfg?: { | ||
throwError?: boolean; | ||
interval?: number; | ||
runs?: number; | ||
}) { | ||
const args = [ | ||
asyncProcessPath, | ||
cfg?.interval ? cfg.interval + '' : '100', | ||
cfg?.runs ? cfg.runs + '' : '4', | ||
cfg?.throwError ? '1' : '0', | ||
]; | ||
return { command: 'node', args }; | ||
} |
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,14 @@ | ||
import { mkdir, rm } from 'node:fs/promises'; | ||
|
||
export async function setupTestFolder(dirName: string) { | ||
await mkdir(dirName, { recursive: true }); | ||
} | ||
|
||
export async function cleanTestFolder(dirName: string) { | ||
await rm(dirName, { recursive: true, force: true }); | ||
await mkdir(dirName, { recursive: true }); | ||
} | ||
|
||
export async function teardownTestFolder(dirName: string) { | ||
await rm(dirName, { recursive: true, force: true }); | ||
} |
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 |
---|---|---|
|
@@ -48,5 +48,5 @@ | |
} | ||
} | ||
}, | ||
"tags": ["scope:tooling"] | ||
"tags": ["scope:shared", "type:tooling"] | ||
} |
Oops, something went wrong.