Skip to content

Commit

Permalink
fix: falling test + skip properly
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed Jan 18, 2025
1 parent 4ca7f98 commit 5a180b9
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ __*
NOTES.md

# testing
/tests/.env
.env

# changelog
# CHANGELOG.md
2 changes: 1 addition & 1 deletion tests/essentials/Client.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assert } from 'chai';
import merge from 'merge-deep';
import merge from 'lodash/merge.js';
import { delay } from '@ircam/sc-utils';

import { Server, ServerContext } from '../../src/server/index.js';
Expand Down
71 changes: 34 additions & 37 deletions tests/essentials/Server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import fs from 'node:fs';
import path from 'node:path';
import * as url from 'node:url';

import { Server as HttpsServer } from 'node:https';
import { Server as HttpServer } from 'node:http';

import { delay } from '@ircam/sc-utils';
import { assert } from 'chai';
import dotenv from 'dotenv';
Expand Down Expand Up @@ -138,12 +141,13 @@ describe('# Server', () => {
});

describe(`## await server.init()`, () => {
it(`should throw if invalid https cert file given`, async () => {
it(`should throw if invalid https cert file given`, async function() {
const envPathname = path.join(__dirname, '.env');

if (!fs.existsSync(envPathname)) {
assert.ok('no .env file, skip this test');
return;
console.log('> no .env file, skip this test');
this.skip();
// return;
}

const envBuffer = fs.readFileSync(envPathname);
Expand All @@ -168,12 +172,12 @@ describe('# Server', () => {
if (!errored) { assert.fail('should have thrown'); }
});

it(`should throw if invalid https key file given`, async () => {
it(`should throw if invalid https key file given`, async function() {
const envPathname = path.join(__dirname, '.env');

if (!fs.existsSync(envPathname)) {
assert.ok('no .env file, skip this test');
return;
console.log('> no .env file, skip this test');
this.skip();
}

const envBuffer = fs.readFileSync(envPathname);
Expand Down Expand Up @@ -210,13 +214,13 @@ describe('# Server', () => {
await server.init();
});

it(`should store self-signed certificated in db`, async () => {
it(`should store self-signed certificated in db`, async function() {
// these test crash the CI for some reason,just ignore them in CI too
const envPathname = path.join(__dirname, '.env');

if (!fs.existsSync(envPathname)) {
assert.ok('no .env file, skip this test');
return;
console.log('> no .env file, skip this test');
this.skip();
}

const selfSignedConfig = merge({}, config);
Expand All @@ -232,17 +236,19 @@ describe('# Server', () => {
assert.isDefined(key);
});

//
// NOTE:
// the subsequent tests will exit early if the `.env` file is not found in the
// tests directory, making it work should be straightforward reading the code...
//
it(`should get infos about valid certificates`, async () => {
it(`should properly create httpServer`, async function() {
const server = new Server(config);
await server.init();

assert.isTrue(server.httpServer instanceof HttpServer);
});

it(`should properly create httpsServer - valid certificates`, async function() {
const envPathname = path.join(__dirname, '.env');

if (!fs.existsSync(envPathname)) {
assert.ok('no .env file, skip this test');
return;
console.log('> no .env file, skip this test');
this.skip();
}

const envBuffer = fs.readFileSync(envPathname);
Expand All @@ -258,20 +264,15 @@ describe('# Server', () => {
const server = new Server(httpsConfig);
await server.init();

assert.notEqual(server.httpsInfos, null);
assert.equal(server.httpsInfos.selfSigned, false);
assert.isDefined(server.httpsInfos.validFrom);
assert.isDefined(server.httpsInfos.validTo);
assert.isDefined(server.httpsInfos.isValid);
assert.isTrue(server.httpServer instanceof HttpsServer);
});

it(`should get infos about self-signed certificates`, async () => {
// these test crash the CI for some reason,just ignore them in CI too
it(`should properly create httpsServer - self-signed certificates`, async function() {
const envPathname = path.join(__dirname, '.env');

if (!fs.existsSync(envPathname)) {
assert.ok('no .env file, skip this test');
return;
console.log('> no .env file, skip this test');
this.skip();
}

const httpsConfig = merge({}, config);
Expand All @@ -281,11 +282,7 @@ describe('# Server', () => {
const server = new Server(httpsConfig);
await server.init();

assert.notEqual(server.httpsInfos, null);
assert.equal(server.httpsInfos.selfSigned, true);
assert.isUndefined(server.httpsInfos.validFrom);
assert.isUndefined(server.httpsInfos.validTo);
assert.isUndefined(server.httpsInfos.isValid);
assert.isTrue(server.httpServer instanceof HttpsServer);
});
});

Expand Down Expand Up @@ -314,13 +311,13 @@ describe('# Server', () => {
});
});

it(`should launch the server (self-signed https)`, async () => {
it(`should launch the server (self-signed https)`, async function() {
// these test crash the CI for some reason,just ignore them in CI too
const envPathname = path.join(__dirname, '.env');

if (!fs.existsSync(envPathname)) {
assert.ok('no .env file, skip this test');
return;
console.log('> no .env file, skip this test');
this.skip();
}

const selfSignedConfig = merge({}, config);
Expand Down Expand Up @@ -354,12 +351,12 @@ describe('# Server', () => {
// the subsequent tests will exit early if the `.env` file is not found in the
// tests directory, making it work should be straightforward reading the code...
//
it(`should launch the server (valid https)`, async () => {
it(`should launch the server (valid https)`, async function() {
const envPathname = path.join(__dirname, '.env');

if (!fs.existsSync(envPathname)) {
assert.ok('no .env file, skip this test');
return;
console.log('> no .env file, skip this test');
this.skip();
}

const envBuffer = fs.readFileSync(envPathname);
Expand Down
113 changes: 113 additions & 0 deletions tests/integration/test-browser.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { fork, exec } from 'node:child_process';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

import { assert } from 'chai';
import puppeteer from 'puppeteer';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const appPath = path.join(__dirname, 'test-browser');

describe.skip('Browser client integration (install, build, start)', () => {
it(`should install deps`, async function() {
this.timeout(60 * 1000);

// delete node modules first
await new Promise(resolve => {
exec(`rm -Rf node_modules`, { cwd: appPath }, (err, stdout, stderr) => {
if (err) {
console.log(err);
assert.fail('"rm -Rf node-modules" error');
return;
}

if (stderr.toString() !== '') {
console.error(stderr);
assert.fail('"rm -Rf node-modules" error');
} else {
assert.ok('"rm -Rf node-modules" success');
}
resolve();
});
});

// delete node modules first
await new Promise(resolve => {
exec(`npm install`, { cwd: appPath }, (err, stdout, stderr) => {
if (err) {
console.log(err);
assert.fail('"npm install" error');
return;
}

if (stderr.toString() !== '') {
console.error(stderr);
assert.ok('"npm install" success but some warning has been triggered');
} else {
assert.ok('"npm install" success');
}

resolve();
});
});
});

it('should build template derived application', async function() {
this.timeout(60 * 1000);

// delete node modules first
await new Promise(resolve => {
exec(`npm run build`, { cwd: appPath }, (err, stdout, stderr) => {
if (err) {
console.log(err);
assert.fail('"npm run build" error');
return;
}

if (stderr.toString() !== '') {
console.error(stderr);
assert.fail('"npm run build" error');
} else {
// console.log(stdout);
assert.ok('"npm run build" success');
}

resolve();
});
});
});

it('should properly launch and start browser client', async function() {
this.timeout(10 * 1000);

// delete node modules first
await new Promise(async (resolve, reject) => {
const serverIndex = path.join(appPath, '.build', 'server', 'index.js');
const forked = fork(serverIndex, { cwd: appPath });
let browser;
let page;

forked.on('message', async msg => {
if (msg === 'soundworks:server:started') {
browser = await puppeteer.launch();
page = await browser.newPage();
await page.goto('http://127.0.0.1:8000');
} else {
try {
const event = JSON.parse(msg);
assert.deepEqual(event, { done: true });
} catch(err) {
assert.fail('received wrong message from client');
}

await browser.close();
forked.kill();
resolve();
}
});
});
});
});

0 comments on commit 5a180b9

Please sign in to comment.