Skip to content

Commit

Permalink
flatten and break up test suites into multiple files (#2015)
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed May 8, 2023
1 parent 1a12d66 commit 89a266d
Show file tree
Hide file tree
Showing 18 changed files with 267 additions and 248 deletions.
56 changes: 56 additions & 0 deletions src/test/configuration/resolution.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { lstatSync } from 'fs';
import { join } from 'path';
import { BIN_CWD_PATH, BIN_PATH, BIN_SCRIPT_PATH, createExec, ctxTsNode, TEST_DIR } from '../helpers';
import { context, expect } from '../testlib';

const exec = createExec({
cwd: TEST_DIR,
});

const test = context(ctxTsNode);

test('should locate tsconfig relative to entry-point by default', async () => {
const r = await exec(`${BIN_PATH} ../a/index`, {
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
});
expect(r.err).toBe(null);
expect(r.stdout).toMatch(/plugin-a/);
});
test('should locate tsconfig relative to entry-point via ts-node-script', async () => {
const r = await exec(`${BIN_SCRIPT_PATH} ../a/index`, {
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
});
expect(r.err).toBe(null);
expect(r.stdout).toMatch(/plugin-a/);
});
test('should locate tsconfig relative to entry-point with --script-mode', async () => {
const r = await exec(`${BIN_PATH} --script-mode ../a/index`, {
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
});
expect(r.err).toBe(null);
expect(r.stdout).toMatch(/plugin-a/);
});
test('should locate tsconfig relative to cwd via ts-node-cwd', async () => {
const r = await exec(`${BIN_CWD_PATH} ../a/index`, {
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
});
expect(r.err).toBe(null);
expect(r.stdout).toMatch(/plugin-b/);
});
test('should locate tsconfig relative to cwd in --cwd-mode', async () => {
const r = await exec(`${BIN_PATH} --cwd-mode ../a/index`, {
cwd: join(TEST_DIR, 'cwd-and-script-mode/b'),
});
expect(r.err).toBe(null);
expect(r.stdout).toMatch(/plugin-b/);
});
test('should locate tsconfig relative to realpath, not symlink, when entrypoint is a symlink', async (t) => {
if (lstatSync(join(TEST_DIR, 'main-realpath/symlink/symlink.tsx')).isSymbolicLink()) {
const r = await exec(`${BIN_PATH} main-realpath/symlink/symlink.tsx`);
expect(r.err).toBe(null);
expect(r.stdout).toBe('');
} else {
t.log('Skipping');
return;
}
});
2 changes: 1 addition & 1 deletion src/test/configuration/tsnode-opts-from-tsconfig.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BIN_PATH } from '../helpers/paths';
import { createExec } from '../exec-helpers';
import { createExec } from '../helpers/exec';
import { TEST_DIR } from '../helpers/paths';
import { context, expect } from '../testlib';
import { join, resolve } from 'path';
Expand Down
35 changes: 35 additions & 0 deletions src/test/create.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ctxTsNode } from './helpers';
import { context, expect } from './testlib';

const test = context(ctxTsNode);

test.suite('create', ({ contextEach }) => {
const test = contextEach(async (t) => {
return {
service: t.context.tsNodeUnderTest.create({
compilerOptions: { target: 'es5' },
skipProject: true,
}),
};
});

test('should create generic compiler instances', (t) => {
const output = t.context.service.compile('const x = 10', 'test.ts');
expect(output).toMatch('var x = 10;');
});

test.suite('should get type information', (test) => {
test('given position of identifier', (t) => {
expect(t.context.service.getTypeInfo('/**jsdoc here*/const x = 10', 'test.ts', 21)).toEqual({
comment: 'jsdoc here',
name: 'const x: 10',
});
});
test('given position that does not point to an identifier', (t) => {
expect(t.context.service.getTypeInfo('/**jsdoc here*/const x = 10', 'test.ts', 0)).toEqual({
comment: '',
name: '',
});
});
});
});
6 changes: 3 additions & 3 deletions src/test/esm-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import {
CMD_ESM_LOADER_WITHOUT_PROJECT,
CMD_TS_NODE_WITHOUT_PROJECT_FLAG,
ctxTsNode,
delay,
nodeSupportsImportAssertions,
nodeSupportsUnflaggedJsonImports,
nodeUsesNewHooksApi,
resetNodeEnvironment,
TEST_DIR,
tsSupportsImportAssertions,
tsSupportsStableNodeNextNode16,
createExec,
createSpawn,
ExecReturn,
} from './helpers';
import { createExec, createSpawn, ExecReturn } from './exec-helpers';
import { join, resolve } from 'path';
import * as expect from 'expect';
import type { NodeLoaderHooksAPI2 } from '../';
Expand Down
2 changes: 1 addition & 1 deletion src/test/exec-helpers.ts → src/test/helpers/exec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ChildProcess, ExecException, ExecOptions, SpawnOptions } from 'child_process';
import { exec as childProcessExec, spawn as childProcessSpawn } from 'child_process';
import { ExpectStream, expectStream } from '@cspotcode/expect-stream';
import { expect } from './testlib';
import { expect } from '../testlib';

export type ExecReturn = Promise<ExecResult> & { child: ChildProcess };
export interface ExecResult {
Expand Down
1 change: 1 addition & 0 deletions src/test/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './paths';
export * from './command-lines';
export * from './reset-node-environment';
export * from './version-checks';
export * from './exec';
Loading

0 comments on commit 89a266d

Please sign in to comment.