Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"eslint-config-prettier": "^9.0.0",
"jest": "^29.7.0",
"jest-environment-node": "^29.7.0",
"jest-util": "^29.7.0",
"nx": "19.8.0",
"prettier": "^2.6.2",
"ts-jest": "^29.1.0",
Expand Down
204 changes: 113 additions & 91 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions test-helpers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# test-helpers

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build test-helpers` to build the library.

## Running unit tests

Run `nx test test-helpers` to execute the unit tests via [Jest](https://jestjs.io).
21 changes: 21 additions & 0 deletions test-helpers/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const baseConfig = require('../eslint.config.js');

module.exports = [
...baseConfig,
{
files: ['**/*.json'],
rules: {
'@nx/dependency-checks': [
'error',
{
ignoredFiles: ['{projectRoot}/eslint.config.{js,cjs,mjs}'],
// TODO: @nx/dependency-checks incorrectly reports unused dependencies
checkObsoleteDependencies: false,
},
],
},
languageOptions: {
parser: require('jsonc-eslint-parser'),
},
},
];
10 changes: 10 additions & 0 deletions test-helpers/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
displayName: 'test-helpers',
preset: '../jest.preset.js',
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../coverage/test-helpers',
};
11 changes: 11 additions & 0 deletions test-helpers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "test-helpers",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not publishable, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, it's a private package

"version": "0.0.1",
"dependencies": {
"tslib": "^2.3.0"
},
"type": "commonjs",
"main": "./src/index.js",
"typings": "./src/index.d.ts",
"private": true
}
19 changes: 19 additions & 0 deletions test-helpers/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "test-helpers",
"$schema": "../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "test-helpers/src",
"projectType": "library",
"tags": [],
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/test-helpers",
"main": "test-helpers/src/index.ts",
"tsConfig": "test-helpers/tsconfig.lib.json",
"assets": ["test-helpers/*.md"]
}
}
}
}
1 change: 1 addition & 0 deletions test-helpers/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/test-helpers';
24 changes: 24 additions & 0 deletions test-helpers/src/lib/test-helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { writeFiles, getTempDirectory, cleanup } from './test-helpers';
import * as fs from 'fs';
import * as path from 'path';

test('writeFiles in temp directory with cleanup', () => {
const directory = getTempDirectory('test_helpers');
const files = {
'package.json': '{}',
'dir/file.js': 'module.exports = "x";',
};

writeFiles(directory, files);

expect(fs.readFileSync(path.join(directory, 'package.json'), 'utf8')).toBe(
'{}'
);
expect(fs.readFileSync(path.join(directory, 'dir', 'file.js'), 'utf8')).toBe(
'module.exports = "x";'
);

cleanup(directory);

expect(fs.existsSync(directory)).toBe(false);
});
40 changes: 40 additions & 0 deletions test-helpers/src/lib/test-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
import { createDirectory } from 'jest-util';

export const getTempDirectory = (name: string) =>
path.resolve(os.tmpdir(), name);

export const cleanup = (directory: string) => {
fs.rmSync(directory, { recursive: true, force: true, maxRetries: 10 });
};

/**
* Creates a nested directory with files and their contents
* writeFiles(
* '/home/tmp',
* {
* 'package.json': '{}',
* 'dir/file.js': 'module.exports = "x";',
* }
* );
*/
export const writeFiles = (
directory: string,
files: { [filename: string]: string | NodeJS.ArrayBufferView }
) => {
createDirectory(directory);

Object.keys(files).forEach((fileOrPath) => {
const dirname = path.dirname(fileOrPath);

if (dirname !== '/') {
createDirectory(path.join(directory, dirname));
}
fs.writeFileSync(
path.resolve(directory, ...fileOrPath.split('/')),
files[fileOrPath]
);
});
};
22 changes: 22 additions & 0 deletions test-helpers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
10 changes: 10 additions & 0 deletions test-helpers/tsconfig.lib.json
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": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"]
}
14 changes: 14 additions & 0 deletions test-helpers/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
}
3 changes: 2 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"@callstack/create-rnef-app": ["packages/create-app/src/index.ts"]
"@callstack/create-rnef-app": ["packages/create-app/src/index.ts"],
"test-helpers": ["test-helpers/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down