Skip to content

Commit

Permalink
Add e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Jun 20, 2024
1 parent 8062892 commit 79959cd
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 15 deletions.
31 changes: 22 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup node ${{ env.PRIMARY_NODEJS_VERSION }}
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: ${{ env.PRIMARY_NODEJS_VERSION }}
cache: "npm"
Expand All @@ -32,9 +32,9 @@ jobs:
- 18.0
- 20
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup node ${{ matrix.node_version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node_version }}
cache: 'npm'
Expand All @@ -55,7 +55,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Send coverage to Coveralls
uses: coverallsapp/github-action@1.1.3
uses: coverallsapp/github-action@v2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true
Expand All @@ -64,15 +64,28 @@ jobs:
name: Test bundle
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup node ${{ env.PRIMARY_NODEJS_VERSION }}
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: ${{ env.PRIMARY_NODEJS_VERSION }}
cache: "npm"
- run: npm ci
- run: npm run bundle-and-test

e2e-tests:
name: E2E tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup node ${{ env.PRIMARY_NODEJS_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.PRIMARY_NODEJS_VERSION }}
cache: "npm"
- run: npm ci
- run: npm run test:e2e

unit-tests:
name: Unit tests
runs-on: ubuntu-latest
Expand All @@ -88,9 +101,9 @@ jobs:
- 22

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Setup node ${{ matrix.node_version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node_version }}
cache: "npm"
Expand Down
1 change: 1 addition & 0 deletions dist/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.js
*.js.map
!.gitignore
!.npmignore
!test/json-ext.js
Expand Down
2 changes: 1 addition & 1 deletion dist/.npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*
!json-ext.js
!json-ext.min.js
!version.js
!package.json
3 changes: 3 additions & 0 deletions dist/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
4 changes: 2 additions & 2 deletions dist/test/json-ext.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global jsonExt */
import fs from 'node:fs';
import assert from 'node:assert';
const fs = require('node:fs');
const assert = require('node:assert');

describe('dist/json-ext.js', () => {
before(() => new Function(fs.readFileSync('dist/json-ext.js'))());
Expand Down
4 changes: 2 additions & 2 deletions dist/test/json-ext.min.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global jsonExt */
import fs from 'node:fs';
import assert from 'node:assert';
const fs = require('node:fs');
const assert = require('node:assert');

describe('dist/json-ext.min.js', () => {
before(() => new Function(fs.readFileSync('dist/json-ext.min.js'))());
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"import": "./src/index.js",
"types": "./index.d.ts"
},
"./dist/*": "./dist/*.js",
"./dist/*": "./dist/*",
"./package.json": "./package.json"
},
"scripts": {
Expand All @@ -39,6 +39,7 @@
"test:all": "npm run test:src && npm run test:cjs && npm run test:dist",
"test:src": "mocha --reporter progress src/*.test.js",
"test:cjs": "mocha --reporter progress cjs/*.test.cjs",
"test:e2e": "mocha --reporter progress test-e2e",
"test:dist": "mocha --reporter progress dist/test",
"bundle-and-test": "npm run bundle && npm run test:dist",
"coverage": "c8 --reporter=lcovonly npm test",
Expand Down
41 changes: 41 additions & 0 deletions test-e2e/commonjs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const assert = require('assert');
const fs = require('fs');

it('basic require', async () => {
const { stringifyInfo } = require('@discoveryjs/json-ext');
const { bytes } = stringifyInfo({ foo: 123 });

assert.strictEqual(bytes, 11);
});

it('should export package.json', async () => {
const packageJson = require('@discoveryjs/json-ext/package.json');

assert.strictEqual(packageJson.name, '@discoveryjs/json-ext');
});

describe('export files', () => {
const files = [
'dist/json-ext.js',
'dist/json-ext.min.js'
];

for (const filename of files) {
it(filename, () => {
const { stringifyInfo } = require(`@discoveryjs/json-ext/${filename}`);
const { bytes } = stringifyInfo({ foo: 123 });

assert.strictEqual(bytes, 11);
});
}
});

it('should not be able to access to files not defined by exports', () => {
const filename = 'cjs/index.cjs';

assert(fs.existsSync(filename), `${filename} should exist`);
assert.throws(
() => require(`@discoveryjs/json-ext/${filename}`),
new RegExp(`Package subpath '\\./${filename}' is not defined by "exports"`)
);
});
41 changes: 41 additions & 0 deletions test-e2e/esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import assert from 'assert';
import fs from 'fs';

it('basic import', async () => {
const { stringifyInfo } = await import('@discoveryjs/json-ext');
const { bytes } = stringifyInfo({ foo: 123 });

assert.strictEqual(bytes, 11);
});

// import attributes (i.e. "import(..., { with ... })") cause syntax error currently, disable the test for now;
// it('package.json', async () => {
// const packageJson = await import('@discoveryjs/json-ext/package.json', { with { type: "json" } }); // should expose package.json
// assert.strictEqual(packageJson.name, '@discoveryjs/json-ext');
// });

describe('export files', () => {
const files = [
'dist/json-ext.js',
'dist/json-ext.min.js'
];

for (const filename of files) {
it(filename, async () => {
const { default: { stringifyInfo } } = await import(`@discoveryjs/json-ext/${filename}`);
const { bytes } = stringifyInfo({ foo: 123 });

assert.strictEqual(bytes, 11);
});
}
});

it('should not be able to access to files not defined by exports', async () => {
const filename = 'src/index.js';

assert(fs.existsSync(filename), `${filename} should exist`);
await assert.rejects(
() => import(`@discoveryjs/json-ext/${filename}`),
new RegExp(`Package subpath '\\./${filename}' is not defined by "exports"`)
);
});

0 comments on commit 79959cd

Please sign in to comment.