From b1880f21ac21eed655f9f3a7fbb1943f1c850c29 Mon Sep 17 00:00:00 2001 From: Comandeer Date: Fri, 14 Jul 2023 01:05:17 +0200 Subject: [PATCH] refactor(createesmlmtest): rewrite to `testEsmlm` macro --- .../testEsmlm.js} | 16 +++++----- tests/esmlm.js | 30 +++++++++---------- 2 files changed, 24 insertions(+), 22 deletions(-) rename tests/__helpers__/{createEsmlmTest.js => macros/testEsmlm.js} (79%) diff --git a/tests/__helpers__/createEsmlmTest.js b/tests/__helpers__/macros/testEsmlm.js similarity index 79% rename from tests/__helpers__/createEsmlmTest.js rename to tests/__helpers__/macros/testEsmlm.js index 7cfe87d..9340473 100644 --- a/tests/__helpers__/createEsmlmTest.js +++ b/tests/__helpers__/macros/testEsmlm.js @@ -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 ) ); @@ -22,17 +23,18 @@ const __dirname = dirname( fileURLToPath( import.meta.url ) ); */ /** + * @param {import('ava').ExecutionContext} 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, @@ -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, @@ -54,6 +56,6 @@ function createEsmlmTest( { return userCallback( t, results ); } } ); -} +} ); -export default createEsmlmTest; +export default testEsmlm; diff --git a/tests/esmlm.js b/tests/esmlm.js index 632ce8e..7c2ec83 100644 --- a/tests/esmlm.js +++ b/tests/esmlm.js @@ -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__' ); @@ -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 } ) { @@ -62,10 +62,10 @@ 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, @@ -73,10 +73,10 @@ test( 'esmlm correctly passes arguments to the underlying program', createEsmlmT 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/; @@ -84,4 +84,4 @@ test( 'esmlm does not duplicate error messages', createEsmlmTest( { t.notRegex( stderr, moduleNotFoundError ); t.is( exitCode, unsuccessfulExitCode ); } -} ) ); +} );