Skip to content

Commit

Permalink
Merge pull request #15 from Comandeer/t/14
Browse files Browse the repository at this point in the history
  • Loading branch information
Comandeer authored Jul 13, 2023
2 parents 60527f1 + b1880f2 commit a6aa3b8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { dirname } from 'node:path';
import { resolve as resolvePath } from 'node:path';
import { fileURLToPath } from 'node:url';
import createCmdTest from './createCmdTest.js';
import test from 'ava';
import testCmd from '../testCmd.js';

const __dirname = dirname( fileURLToPath( import.meta.url ) );

Expand All @@ -22,17 +23,18 @@ const __dirname = dirname( fileURLToPath( import.meta.url ) );
*/

/**
* @param {import('ava').ExecutionContext<unknown>} t Test execution context
* @param {EsmlmTestOptions} options
* @returns {() => Promise}
*/
function createEsmlmTest( {
const testEsmlm = test.macro( ( t, {
cwd,
entryPoint,
args = [],
env = {},
callback: userCallback
} = {} ) {
const esmlmPath = resolvePath( __dirname, '..', '..', 'bin', 'esmlm.js' );
} = {} ) => {
const esmlmPath = resolvePath( __dirname, '..', '..', '..', 'bin', 'esmlm.js' );
const cmd = esmlmPath;
const params = entryPoint ? [
entryPoint,
Expand All @@ -45,7 +47,7 @@ function createEsmlmTest( {
// For some reason it breaks the coverage calculation.
env.NODE_V8_COVERAGE = '';

return createCmdTest( {
return testCmd( t, {
cmd,
params,
env,
Expand All @@ -54,6 +56,6 @@ function createEsmlmTest( {
return userCallback( t, results );
}
} );
}
} );

export default createEsmlmTest;
export default testEsmlm;
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { dirname } from 'node:path';
import { resolve as resolvePath } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import createCmdTest from './createCmdTest.js';
import test from 'ava';
import testCmd from '../testCmd.js';

const __dirname = dirname( fileURLToPath( import.meta.url ) );

Expand All @@ -21,16 +22,17 @@ const __dirname = dirname( fileURLToPath( import.meta.url ) );
*/

/**
* @param {import('ava').ExecutionContext<unknown>} t Test execution context
* @param {LoaderTestOptions} options
* @returns {() => Promise}
*/
function createLoaderTest( {
const testLoader = test.macro( ( t, {
fixturePath,
entryPoint = fixturePath,
env = {},
callback: userCallback
} = {} ) {
const loaderPath = resolvePath( __dirname, '..', '..', 'src', 'index.js' );
} = {} ) => {
const loaderPath = resolvePath( __dirname, '..', '..', '..', 'src', 'index.js' );
const loaderURL = pathToFileURL( loaderPath );
const cmd = 'node';
const params = [
Expand All @@ -39,7 +41,7 @@ function createLoaderTest( {
entryPoint
];

return createCmdTest( {
return testCmd( t, {
cmd,
params,
env,
Expand All @@ -48,6 +50,6 @@ function createLoaderTest( {
return userCallback( t, results );
}
} );
}
} );

export default createLoaderTest;
export default testLoader;
27 changes: 13 additions & 14 deletions tests/__helpers__/createCmdTest.js → tests/__helpers__/testCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,29 @@ import { execa } from 'execa';
*/

/**
* @param {import('ava').ExecutionContext<unknown>} t Test execution context
* @param {CmdTestOptions} options
* @returns {() => Promise}
*/
function createCmdTest( {
async function testCmd( t, {
cmd,
callback,
params = [],
env = {},
cwd = processCWD()
} = {} ) {
return async ( t ) => {
let result;
let result;

try {
result = await execa( cmd, params, {
cwd,
env
} );
} catch ( error ) {
result = error;
}
try {
result = await execa( cmd, params, {
cwd,
env
} );
} catch ( error ) {
result = error;
}

return callback( t, result );
};
return callback( t, result );
}

export default createCmdTest;
export default testCmd;
30 changes: 15 additions & 15 deletions tests/esmlm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { dirname } from 'node:path';
import { resolve as resolvePath } from 'node:path';
import { fileURLToPath } from 'node:url';
import test from 'ava';
import createEsmlmTest from './__helpers__/createEsmlmTest.js';
import testEsmlm from './__helpers__/macros/testEsmlm.js';

const __dirname = dirname( fileURLToPath( import.meta.url ) );
const fixtureDirPath = resolvePath( __dirname, '__fixtures__' );
Expand All @@ -18,42 +18,42 @@ const sampleArgs = [
'--some-arg'
];

test( 'esmlm correctly launches Node.js module without any parameters', createEsmlmTest( {
test( 'esmlm correctly launches Node.js module without any parameters', testEsmlm, {
cwd: esmlmFixturePath,
callback( t, { stdout, exitCode } ) {
t.is( stdout, 'true' );
t.is( exitCode, successfulExitCode );
}
} ) );
} );

test( 'esmlm correctly launches Node.js module with the absolute path as the parameter', createEsmlmTest( {
test( 'esmlm correctly launches Node.js module with the absolute path as the parameter', testEsmlm, {
cwd: esmlmFixturePath,
entryPoint: esmlmFixtureEntryPointPath,
callback( t, { stdout, exitCode } ) {
t.is( stdout, 'true' );
t.is( exitCode, successfulExitCode );
}
} ) );
} );

test( 'esmlm correctly launches Node.js module with the relative path as the parameter', createEsmlmTest( {
test( 'esmlm correctly launches Node.js module with the relative path as the parameter', testEsmlm, {
cwd: esmlmFixturePath,
entryPoint: 'index.js',
callback( t, { stdout, exitCode } ) {
t.is( stdout, 'true' );
t.is( exitCode, successfulExitCode );
}
} ) );
} );

test( 'esmlm correctly launches Node.js module with the "." as the parameter', createEsmlmTest( {
test( 'esmlm correctly launches Node.js module with the "." as the parameter', testEsmlm, {
cwd: esmlmFixturePath,
entryPoint: '.',
callback( t, { stdout, exitCode } ) {
t.is( stdout, 'true' );
t.is( exitCode, successfulExitCode );
}
} ) );
} );

test( 'esmlm throws error when incorrect relative path is passed as the parameter', createEsmlmTest( {
test( 'esmlm throws error when incorrect relative path is passed as the parameter', testEsmlm, {
cwd: esmlmFixturePath,
entryPoint: './non-existent.js',
callback( t, { stderr, exitCode } ) {
Expand All @@ -62,26 +62,26 @@ test( 'esmlm throws error when incorrect relative path is passed as the paramete
t.regex( stderr, moduleNotFoundError );
t.not( exitCode, 0 );
}
} ) );
} );

// #2
test( 'esmlm correctly passes arguments to the underlying program', createEsmlmTest( {
test( 'esmlm correctly passes arguments to the underlying program', testEsmlm, {
cwd: esmlmArgsFixturePath,
entryPoint: esmlmArgsFixtureEntryPointPath,
args: sampleArgs,
callback( t, { stdout, exitCode } ) {
t.is( stdout, sampleArgs.join( ' ' ) );
t.is( exitCode, successfulExitCode );
}
} ) );
} );

// #1
test( 'esmlm does not duplicate error messages', createEsmlmTest( {
test( 'esmlm does not duplicate error messages', testEsmlm, {
cwd: esmlmErrorFixturePath,
callback( t, { stderr, exitCode } ) {
const moduleNotFoundError = /Error: Command failed with exit code 1/;

t.notRegex( stderr, moduleNotFoundError );
t.is( exitCode, unsuccessfulExitCode );
}
} ) );
} );
58 changes: 28 additions & 30 deletions tests/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { join as joinPath } from 'node:path';
import { resolve as resolvePath } from 'node:path';
import { fileURLToPath } from 'node:url';
import test from 'ava';
import createLoaderTest from './__helpers__/createLoaderTest.js';
import testLoader from './__helpers__/macros/testLoader.js';

const __dirname = dirname( fileURLToPath( import.meta.url ) );
const fixtureDirPath = resolvePath( __dirname, '__fixtures__' );
Expand Down Expand Up @@ -49,88 +49,88 @@ test.after.always( async () => {
} );
} );

test( 'loader raises an error if loaders\' definitions are not found', createLoaderTest( {
test( 'loader raises an error if loaders\' definitions are not found', testLoader, {
fixturePath: withoutLoaderFileFixturePath,
callback( t, { stderr } ) {
const errorRegex = /ESMLM:.*?The file with loaders' definition was not found./;

t.regex( stderr, errorRegex );
}
} ) );
} );

test( 'loader correctly uses user-provided loader', createLoaderTest( {
test( 'loader correctly uses user-provided loader', testLoader, {
fixturePath: simpleLoaderFixturePath,
callback( t, { stdout } ) {
t.is( stdout, 'hublabubla' );
}
} ) );
} );

test( 'path to the config file can be passed as environment variable (relative one)', createLoaderTest( {
test( 'path to the config file can be passed as environment variable (relative one)', testLoader, {
fixturePath: customConfigFileFixturePath,
env: {
ESMLM_CONFIG: './customConfig.mjs'
},
callback( t, { stdout } ) {
t.is( stdout, 'true' );
}
} ) );
} );

test( 'path to the config file can be passed as environment variable (absolute one)', createLoaderTest( {
test( 'path to the config file can be passed as environment variable (absolute one)', testLoader, {
fixturePath: customConfigFileFixturePath,
env: {
ESMLM_CONFIG: resolvePath( customConfigFileFixturePath, 'customConfig.mjs' )
},
callback( t, { stdout } ) {
t.is( stdout, 'true' );
}
} ) );
} );

test( 'module config file works correctly', createLoaderTest( {
test( 'module config file works correctly', testLoader, {
fixturePath: moduleConfigFileFixturePath,
callback( t, { stdout } ) {
t.is( stdout, 'true' );
}
} ) );
} );

test( 'nested module uses correct config file', createLoaderTest( {
test( 'nested module uses correct config file', testLoader, {
fixturePath: nestedLoaderLevel1DirPath,
entryPoint: 'someNestedModule.js',
callback( t, { stdout } ) {
t.is( stdout, 'true' );
}
} ) );
} );

test( 'deeply nested module uses correct config file', createLoaderTest( {
test( 'deeply nested module uses correct config file', testLoader, {
fixturePath: nestedLoaderLevel3DirPath,
entryPoint: 'superDeeplyNested.js',
callback( t, { stdout } ) {
t.is( stdout, 'nested' );
}
} ) );
} );

test( 'loader ignores modules loaded from the outside of project root', createLoaderTest( {
test( 'loader ignores modules loaded from the outside of project root', testLoader, {
fixturePath: nestedProjectRootDirPath,
callback( t, { stdout } ) {
t.is( stdout, 'false' );
}
} ) );
} );

test( 'both resolver and loader are matched against module URL', createLoaderTest( {
test( 'both resolver and loader are matched against module URL', testLoader, {
fixturePath: resolvingURLsFixturePath,
callback( t, { stdout } ) {
t.is( stdout, 'true' );
}
} ) );
} );

test( 'loader() receives correct arguments', createLoaderTest( {
test( 'loader() receives correct arguments', testLoader, {
fixturePath: loaderArgsFixturePath,
callback( t, { stdout } ) {
t.is( stdout, 'true' );
}
} ) );
} );

test( 'loader without a project root restricts itself to CWD', ( t ) => {
const currentTest = createLoaderTest( {
return testLoader.exec( t, {
fixturePath: noProjectRootFixturePath,
entryPoint: 'index.mjs',
callback( t, { stdout, stderr } ) {
Expand All @@ -140,27 +140,25 @@ test( 'loader without a project root restricts itself to CWD', ( t ) => {
t.true( stderr.includes( projectRootNotDetectedError ) );
}
} );

return currentTest( t );
} );

test( 'modules inside node_modules are ignored', createLoaderTest( {
test( 'modules inside node_modules are ignored', testLoader, {
fixturePath: npmImportsFixturePath,
callback( t, { stdout } ) {
t.is( stdout, 'false\nfalse' );
}
} ) );
} );

test( 'built-in modules are ignored', createLoaderTest( {
test( 'built-in modules are ignored', testLoader, {
fixturePath: builinModulesFixturePath,
callback( t, { stdout } ) {
t.not( stdout, 'true\ntrue\ntrue' );
}
} ) );
} );

test( 'loaded module is passed through all matched loaders in order', createLoaderTest( {
test( 'loaded module is passed through all matched loaders in order', testLoader, {
fixturePath: multipleLoadersFixturePath,
callback( t, { stdout } ) {
t.not( stdout, 'babubla' );
}
} ) );
} );

0 comments on commit a6aa3b8

Please sign in to comment.