Skip to content

Commit

Permalink
Merge pull request #5 from soundcut/add-test-framework
Browse files Browse the repository at this point in the history
Add test framework
  • Loading branch information
ziir authored Aug 26, 2020
2 parents cb8f22e + 338e1b2 commit 994848c
Show file tree
Hide file tree
Showing 10 changed files with 5,630 additions and 24 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Install > Build > Test

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Using Node.js 12
uses: actions/setup-node@v1
with:
node-version: 12.x
- name: Install
run: npm ci
- name: Build
run: npm run build:production
- name: Install Test Dependencies
run: cd test; npm ci;
- name: Test
run: npm test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/*
**/node_modules/*
.vscode/
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
"dist"
],
"scripts": {
"build": "rollup -c",
"build:production": "NODE_ENV=production rollup -c",
"test": "echo \"Error: no test specified\" && exit 1"
"clean": "rm -rf dist",
"build": "npm run clean; rollup -c",
"build:production": "npm run clean; NODE_ENV=production rollup -c",
"test": "npm run build:production; cd test; npm t"
},
"author": {
"name": "Timothée 'Tim' Pillard",
Expand Down
24 changes: 21 additions & 3 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async function fetchSample(url) {
const response = await fetchPromise;
if (response) {
if (response.status !== 200) {
const error = new Error('Unable to fetch slice');
const error = new Error('Unable to fetch sample');
error.response = response;
throw error;
}
Expand All @@ -35,5 +35,23 @@ function getArrayBuffer(file) {
});
}

window.fetchSample = fetchSample;
window.getArrayBuffer = getArrayBuffer;
async function test(which, what) {
const start = performance.now();

try {
const ret = await what();

const end = performance.now();
const out = { case: which, duration: `${end - start}`, length: ret.length };
console.table(out);
return out;
} catch (_err) {
const msg = `Test failed > ${which} > ${_err.message.replace(
'Error: ',
''
)}`;
const err = new Error();
err.stack = [msg, ..._err.stack.split('\n').slice(1)].join('\n');
throw err;
}
}
31 changes: 24 additions & 7 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,31 @@
<html>
<head>
<script src="../dist/decode-audio-data-fast.standalone.umd.js"></script>
<script src="helpers.js"></script>
<script src="index.js"></script>
<script>
if (!self.DADF) {
throw new Error('window.DADF from UMD <script> unavailable.');
}

if (typeof self.DADF.getFileAudioBuffer !== 'function') {
throw new Error(
'window.DADF.getFileAudioBuffer (UMD) function unavailable.'
);
}
</script>
<script type="module">
import { getFileAudioBuffer as gFAB } from '../dist/decode-audio-data-fast.standalone.esm.js';
self.gFAB = gFAB;
if (!gFAB) {
throw new Error(
'getFileAudioBuffer import from ESM Module unavailable.'
);
}

if (typeof gFAB !== 'function') {
throw new Error('gFAB (ESM) function unavailable.');
}
</script>
<script src="helpers.js"></script>
<script src="index.js"></script>
</head>
<body>

</body>
</html>
<body></body>
</html>
11 changes: 0 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
async function test(which, what) {
const start = performance.now();

const ret = await what();

const end = performance.now();
const out = { case: which, duration: `${end - start}`, length: ret.length };
console.table(out);
return out;
}

async function run() {
const file = await fetchSample('../samples/1.mp3');

Expand Down
29 changes: 29 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const http = require('http');
const { Server } = require('node-static');
require('expect-puppeteer');

const port = 3002;
const url = `http://localhost:${port}/test/`;

let server;

describe('decode-audio-data-fast tests in headless browser', () => {
let onPageError;
beforeAll(async () => {
const staticServer = new Server('..');
server = http
.createServer((req, res) => staticServer.serve(req, res))
.listen(port);

onPageError = jest.fn();
page.on('pageerror', onPageError);
await page.goto(url);
await page.waitFor(4000);
});

it('should no throw any error', async () => {
await expect(onPageError).not.toBeCalled();
});

afterAll(() => server.close());
});
3 changes: 3 additions & 0 deletions test/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
preset: 'jest-puppeteer',
};
Loading

0 comments on commit 994848c

Please sign in to comment.