diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5cba2aac7..03a5b9e23 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -21,4 +21,4 @@ jobs: - name: Install dependencies run: npm i - name: Running tests - run: npm run test-core-plugins \ No newline at end of file + run: npm test \ No newline at end of file diff --git a/config.test_with_custom_plugins.js b/config.test_with_custom_plugins.js index 690852dbe..fbe27825a 100644 --- a/config.test_with_custom_plugins.js +++ b/config.test_with_custom_plugins.js @@ -1,4 +1,10 @@ - export default { +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +export default { CUSTOM_PLUGINS_PATH: __dirname + '/test/fixtures/custom-plugins', diff --git a/package.json b/package.json index eabc367bd..edc00f9f9 100644 --- a/package.json +++ b/package.json @@ -60,9 +60,10 @@ "iframely-proxy-plugins": true, "main": "./lib/core.js", "scripts": { - "test": "npm run test-core-plugins && npm run test-e2e", + "test": "npm run test-core-plugins && npm run test-e2e && npm run custom_plugins", "test-core-plugins": "mocha --exit test/core-plugins.js", - "test-e2e": "NODE_ENV=test PORT=8080 mocha --exit test/e2e.js" + "test-e2e": "NODE_ENV=test PORT=8080 mocha --exit test/e2e.js", + "custom_plugins": "NODE_ENV=test_with_custom_plugins PORT=8080 mocha --exit test/custom_plugins.js" }, "engines": { "node": ">=14.0" diff --git a/test/custom_plugins.js b/test/custom_plugins.js index b8b280f9f..0c879212b 100644 --- a/test/custom_plugins.js +++ b/test/custom_plugins.js @@ -1,34 +1,23 @@ 'use strict'; -import * as request from 'supertest'; -import * as ServerMock from 'mock-http-server'; -import * as chai from 'chai'; -import * as async from 'async'; - -var server; - -var ENV_WITH_CUSTOM_PLUGINS = 'test_with_custom_plugins'; - -function invalidateRequireCache () { - Object.keys(require.cache).forEach(function(key) { delete require.cache[key] }) -} - -var startWithENV = function (env, cb) { - process.env.NODE_ENV = env; -import * as app from '../app.js'; - server = app.listen(process.env.PORT, cb); -}; +import request from 'supertest'; +import ServerMock from 'mock-http-server'; +import chai from 'chai'; +import app from '../app.js'; describe('custom plugins', function() { + var BASE_IFRAMELY_SERVER_URL = 'http://localhost:' + process.env.PORT; var TARGET_MOCKED_SERVER_PORT = 9000; var TARGET_MOCKED_SERVER_BASEURL = 'http://127.0.0.1:' + TARGET_MOCKED_SERVER_PORT; - var targetMockedServer = new ServerMock({ host: 'localhost', port: TARGET_MOCKED_SERVER_PORT }); + var targetMockedServer = new ServerMock({ host: '127.0.0.1', port: TARGET_MOCKED_SERVER_PORT }); + var server; beforeEach(function(done) { - invalidateRequireCache(); - targetMockedServer.start(done); + server = app.listen(process.env.PORT, function() { + targetMockedServer.start(done); + }); }); afterEach(function(done) { @@ -38,7 +27,7 @@ describe('custom plugins', function() { }); it('should use a custom plugin if defined', function(done) { - startWithENV(ENV_WITH_CUSTOM_PLUGINS, function () { + targetMockedServer.on({ method: 'GET', path: '/testok', @@ -56,33 +45,31 @@ describe('custom plugins', function() { chai.expect(res.body.meta.description).to.equal('custom description for test.com domain'); done(err); }); - }); }); - //it('should use a core plugin if no custom plugin exists', function(done) { - // startWithENV('test', function () { - // targetMockedServer.on({ - // method: 'GET', - // path: '/testok', - // reply: { - // status: 200, - // headers: { 'content-type': 'text/html' }, - // body: "my titleHi there!" - // } - // }); - // - // var url = TARGET_MOCKED_SERVER_BASEURL + '/testok'; - // request(BASE_IFRAMELY_SERVER_URL) - // .get('/iframely?url=' + url) - // .end(function(err, res) { - // chai.expect(res.body.meta.title).to.equal('my title'); - // done(err); - // }); - // }); - //}); + it('should use a core plugin if no custom plugin exists', function(done) { + + targetMockedServer.on({ + method: 'GET', + path: '/test-another', + reply: { + status: 200, + headers: { 'content-type': 'text/html' }, + body: "my titleHi there!" + } + }); + + var url = TARGET_MOCKED_SERVER_BASEURL + '/test-another'; + request(BASE_IFRAMELY_SERVER_URL) + .get('/iframely?url=' + url) + .end(function(err, res) { + chai.expect(res.body.meta.description).to.equal('my description'); + done(err); + }); + }); it('should use a custom plugin overriding a core plugin ', function(done) { - startWithENV(ENV_WITH_CUSTOM_PLUGINS, function () { + targetMockedServer.on({ method: 'GET', path: '/testok', @@ -100,6 +87,5 @@ describe('custom plugins', function() { chai.expect(res.body.meta.title).to.equal('TITLE FROM CUSTOM-PLUGIN'); done(err); }); - }); }); }); diff --git a/test/e2e.js b/test/e2e.js index d89674299..d3340124f 100644 --- a/test/e2e.js +++ b/test/e2e.js @@ -12,7 +12,7 @@ describe('meta endpoint', function() { var TARGET_MOCKED_SERVER_PORT = 9000; var TARGET_MOCKED_SERVER_BASEURL = 'http://127.0.0.1:' + TARGET_MOCKED_SERVER_PORT; - var targetMockedServer = new ServerMock({ host: 'localhost', port: TARGET_MOCKED_SERVER_PORT }); + var targetMockedServer = new ServerMock({ host: '127.0.0.1', port: TARGET_MOCKED_SERVER_PORT }); var server; beforeEach(function(done) {