Skip to content

Commit 7b17829

Browse files
committed
fix custom plugins test
1 parent 6f2a58e commit 7b17829

File tree

3 files changed

+41
-48
lines changed

3 files changed

+41
-48
lines changed

config.test_with_custom_plugins.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
export default {
1+
import { fileURLToPath } from 'url';
2+
import { dirname } from 'path';
3+
4+
const __filename = fileURLToPath(import.meta.url);
5+
const __dirname = dirname(__filename);
6+
7+
export default {
28

39
CUSTOM_PLUGINS_PATH: __dirname + '/test/fixtures/custom-plugins',
410

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
"scripts": {
6363
"test": "npm run test-core-plugins && npm run test-e2e",
6464
"test-core-plugins": "mocha --exit test/core-plugins.js",
65-
"test-e2e": "NODE_ENV=test PORT=8080 mocha --exit test/e2e.js"
65+
"test-e2e": "NODE_ENV=test PORT=8080 mocha --exit test/e2e.js",
66+
"custom_plugins": "NODE_ENV=test_with_custom_plugins PORT=8080 mocha --exit test/custom_plugins.js"
6667
},
6768
"engines": {
6869
"node": ">=14.0"

test/custom_plugins.js

+32-46
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
11
'use strict';
2-
import * as request from 'supertest';
3-
import * as ServerMock from 'mock-http-server';
4-
import * as chai from 'chai';
5-
import * as async from 'async';
6-
7-
var server;
8-
9-
var ENV_WITH_CUSTOM_PLUGINS = 'test_with_custom_plugins';
10-
11-
function invalidateRequireCache () {
12-
Object.keys(require.cache).forEach(function(key) { delete require.cache[key] })
13-
}
14-
15-
var startWithENV = function (env, cb) {
16-
process.env.NODE_ENV = env;
17-
import * as app from '../app.js';
18-
server = app.listen(process.env.PORT, cb);
19-
};
2+
import request from 'supertest';
3+
import ServerMock from 'mock-http-server';
4+
import chai from 'chai';
5+
import app from '../app.js';
206

217
describe('custom plugins', function() {
8+
229
var BASE_IFRAMELY_SERVER_URL = 'http://localhost:' + process.env.PORT;
2310

2411
var TARGET_MOCKED_SERVER_PORT = 9000;
2512
var TARGET_MOCKED_SERVER_BASEURL = 'http://127.0.0.1:' + TARGET_MOCKED_SERVER_PORT;
2613

27-
var targetMockedServer = new ServerMock({ host: 'localhost', port: TARGET_MOCKED_SERVER_PORT });
14+
var targetMockedServer = new ServerMock({ host: '127.0.0.1', port: TARGET_MOCKED_SERVER_PORT });
15+
var server;
2816

2917
beforeEach(function(done) {
30-
invalidateRequireCache();
31-
targetMockedServer.start(done);
18+
server = app.listen(process.env.PORT, function() {
19+
targetMockedServer.start(done);
20+
});
3221
});
3322

3423
afterEach(function(done) {
@@ -38,7 +27,7 @@ describe('custom plugins', function() {
3827
});
3928

4029
it('should use a custom plugin if defined', function(done) {
41-
startWithENV(ENV_WITH_CUSTOM_PLUGINS, function () {
30+
4231
targetMockedServer.on({
4332
method: 'GET',
4433
path: '/testok',
@@ -56,33 +45,31 @@ describe('custom plugins', function() {
5645
chai.expect(res.body.meta.description).to.equal('custom description for test.com domain');
5746
done(err);
5847
});
59-
});
6048
});
6149

62-
//it('should use a core plugin if no custom plugin exists', function(done) {
63-
// startWithENV('test', function () {
64-
// targetMockedServer.on({
65-
// method: 'GET',
66-
// path: '/testok',
67-
// reply: {
68-
// status: 200,
69-
// headers: { 'content-type': 'text/html' },
70-
// body: "<html><title>my title</title><meta name='description' content='my description'><body>Hi there!</body></html>"
71-
// }
72-
// });
73-
//
74-
// var url = TARGET_MOCKED_SERVER_BASEURL + '/testok';
75-
// request(BASE_IFRAMELY_SERVER_URL)
76-
// .get('/iframely?url=' + url)
77-
// .end(function(err, res) {
78-
// chai.expect(res.body.meta.title).to.equal('my title');
79-
// done(err);
80-
// });
81-
// });
82-
//});
50+
it('should use a core plugin if no custom plugin exists', function(done) {
51+
52+
targetMockedServer.on({
53+
method: 'GET',
54+
path: '/test-another',
55+
reply: {
56+
status: 200,
57+
headers: { 'content-type': 'text/html' },
58+
body: "<html><title>my title</title><meta name='description' content='my description'><body>Hi there!</body></html>"
59+
}
60+
});
61+
62+
var url = TARGET_MOCKED_SERVER_BASEURL + '/test-another';
63+
request(BASE_IFRAMELY_SERVER_URL)
64+
.get('/iframely?url=' + url)
65+
.end(function(err, res) {
66+
chai.expect(res.body.meta.description).to.equal('my description');
67+
done(err);
68+
});
69+
});
8370

8471
it('should use a custom plugin overriding a core plugin ', function(done) {
85-
startWithENV(ENV_WITH_CUSTOM_PLUGINS, function () {
72+
8673
targetMockedServer.on({
8774
method: 'GET',
8875
path: '/testok',
@@ -100,6 +87,5 @@ describe('custom plugins', function() {
10087
chai.expect(res.body.meta.title).to.equal('TITLE FROM CUSTOM-PLUGIN');
10188
done(err);
10289
});
103-
});
10490
});
10591
});

0 commit comments

Comments
 (0)