Skip to content

test: add package import test cases #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 6, 2023
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 .knip.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"project": ["lib/**/*.js!", "tests/**/*.js"],
"ignore": ["tests/fixture/**"],
"ignoreDependencies": [
"@insurgent/export-map-test",
"@semantic-release/commit-analyzer",
"@semantic-release/github",
"@semantic-release/npm",
Expand Down
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 10 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
"node": ">=16.20"
},
"scripts": {
"test": "c8 npm run test:all && npm run knip",
"test:all": "npm run test:ava && npm run test:fuzz && npm run test:tsd",
"test:ava": "ava tests/e2e.test.js",
"test": "npm run test:all && npm run knip",
"test:all": "npm run test:ava && npm run test:nested && npm run test:fuzz && npm run test:tsd",
"test:ava": "c8 ava tests/e2e.test.js",
"test:nested": "cd tests/fixture/nested/ && npm test",
"test:fuzz": "cross-env NODE_OPTIONS=--no-warnings ava tests/fuzz.test.js",
"test:tsd": "tsd --files tests/**/*.test-d.ts",
"knip": "knip",
"lint": "xo",
"lint:fix": "xo --fix"
"lint:fix": "xo --fix",
"postinstall": "cd tests/fixture/nested/ && npm ci"
},
"files": [
"index.js",
Expand All @@ -43,6 +45,7 @@
],
"devDependencies": {
"@fast-check/ava": "1.1.6",
"@insurgent/export-map-test": "1.0.0",
"@insurgentlab/conventional-changelog-preset": "7.0.0",
"@semantic-release/changelog": "6.0.3",
"@semantic-release/git": "10.0.1",
Expand Down Expand Up @@ -75,9 +78,9 @@
"text",
"lcov"
],
"statements": 95,
"branches": 85,
"statements": 100,
"branches": 100,
"functions": 100,
"lines": 95
"lines": 100
}
}
49 changes: 17 additions & 32 deletions tests/e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,20 @@

import test from 'ava';
import importFrom from '../index.js';

const testImportFrom = async (t, dir, file, ext) => {
const extString = ext ? `.${ext}` : '';
const moduleId = `./${file}${extString}`;
const nonExistentModuleId = `./nonexistent${extString}`;

t.is(await importFrom(dir, moduleId), 'unicorn');

const moduleNotFoundError = await t.throwsAsync(importFrom(dir, nonExistentModuleId));
t.is(moduleNotFoundError.code, 'MODULE_NOT_FOUND');
t.regex(moduleNotFoundError.message, new RegExp(`^Cannot find module '${nonExistentModuleId}'`));
};

test('importFrom() (CJS module in CJS folder - no extension)', t => testImportFrom(t, 'tests/fixture/commonjs', 'fixture-cjs', undefined));
test('importFrom() (CJS module in CJS folder - .js extension)', t => testImportFrom(t, 'tests/fixture/commonjs', 'fixture-cjs', 'js'));
test('importFrom() (CJS module in ESM folder - no extension)', t => testImportFrom(t, 'tests/fixture/module', 'fixture-cjs', undefined));
test('importFrom() (CJS module in ESM folder - .cjs extension)', t => testImportFrom(t, 'tests/fixture/module', 'fixture-cjs', 'cjs'));

test('importFrom() (ESM module in ESM folder - no extension)', t => testImportFrom(t, 'tests/fixture/module', 'fixture-esm', undefined));
test('importFrom() (ESM module in ESM folder - .js extension)', t => testImportFrom(t, 'tests/fixture/module', 'fixture-esm', 'js'));
test('importFrom() (ESM module in CJS folder - no extension)', t => testImportFrom(t, 'tests/fixture/commonjs', 'fixture-esm', undefined));
test('importFrom() (ESM module in CJS folder - .mjs extension)', t => testImportFrom(t, 'tests/fixture/commonjs', 'fixture-esm', 'mjs'));

test('importFrom.silent() (CJS)', async t => {
t.is(await importFrom.silent('tests/fixture/module', './fixture-cjs.cjs'), 'unicorn');
t.is(await importFrom.silent('tests/fixture/module', './nonexistent'), undefined);
});

test('importFrom.silent() (ESM)', async t => {
t.is(await importFrom.silent('tests/fixture/module', './fixture-esm.js'), 'unicorn');
t.is(await importFrom.silent('tests/fixture/module', './nonexistent'), undefined);
});
import { testImportFromLocal, testImportFromPackage } from './test-helpers.js'; // eslint-disable-line ava/no-import-test-files

test('local - CJS module in CJS folder - no extension', t => testImportFromLocal(t, importFrom, 'tests/fixture/commonjs', 'fixture-cjs', undefined));
test('local - CJS module in CJS folder - .js extension', t => testImportFromLocal(t, importFrom, 'tests/fixture/commonjs', 'fixture-cjs', 'js'));
test('local - CJS module in ESM folder - no extension', t => testImportFromLocal(t, importFrom, 'tests/fixture/module', 'fixture-cjs', undefined));
test('local - CJS module in ESM folder - .cjs extension', t => testImportFromLocal(t, importFrom, 'tests/fixture/module', 'fixture-cjs', 'cjs'));

test('local - ESM module in ESM folder - no extension', t => testImportFromLocal(t, importFrom, 'tests/fixture/module', 'fixture-esm', undefined));
test('local - ESM module in ESM folder - .js extension', t => testImportFromLocal(t, importFrom, 'tests/fixture/module', 'fixture-esm', 'js'));
test('local - ESM module in CJS folder - no extension', t => testImportFromLocal(t, importFrom, 'tests/fixture/commonjs', 'fixture-esm', undefined));
test('local - ESM module in CJS folder - .mjs extension', t => testImportFromLocal(t, importFrom, 'tests/fixture/commonjs', 'fixture-esm', 'mjs'));

test('package - main export', t => testImportFromPackage(t, importFrom, 'tests/fixture/', '@insurgent/export-map-test', 'main'));
test('package - simple export', t => testImportFromPackage(t, importFrom, 'tests/fixture/', '@insurgent/export-map-test/simple', 'simple'));
test('package - conditional export', t => testImportFromPackage(t, importFrom, 'tests/fixture/', '@insurgent/export-map-test/conditional', 'conditional-import'));
test('package - wildcard export', t => testImportFromPackage(t, importFrom, 'tests/fixture/', '@insurgent/export-map-test/wildcard/js.js', 'wildcard-one'));
test('package - extension wildcard export', t => testImportFromPackage(t, importFrom, 'tests/fixture/', '@insurgent/export-map-test/wildcard-js/one', 'wildcardjs-one'));
7 changes: 7 additions & 0 deletions tests/fixture/nested/nested.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* eslint-disable ava/no-inline-assertions */

import test from 'ava';
import importFrom from '../../../index.js';
import { testImportFromPackage } from '../../test-helpers.js'; // eslint-disable-line ava/no-import-test-files

test('package - loading from parent node_modules', t => testImportFromPackage(t, importFrom, '../../../tests/fixture/', '@insurgent/export-map-test', 'main'));
Loading