Skip to content

Commit

Permalink
refactor(createesmlmtest): rewrite to testEsmlm macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Comandeer committed Jul 13, 2023
1 parent 6a2230a commit b1880f2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 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;
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 );
}
} ) );
} );

0 comments on commit b1880f2

Please sign in to comment.