Skip to content
This repository was archived by the owner on Mar 28, 2022. It is now read-only.

Commit 4c7ba71

Browse files
committed
Fix acceptance tests
1 parent 937b35a commit 4c7ba71

15 files changed

Lines changed: 157 additions & 105 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# tests
1010
/coverage
1111
/mocks
12-
/test/acceptance/main/fixtures/files-watch
12+
/test/acceptance/fixtures/files-watch
1313

1414
# misc
1515
.DS_Store

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Please refer to the [project documentation website][website-url]:
1515

1616
* [Get started](https://www.mocks-server.org/docs/get-started-intro)
1717
* [Tutorials](https://www.mocks-server.org/docs/tutorials-static)
18-
* [Configuration](https://www.mocks-server.org/docs/configuration-command-line-arguments)
18+
* [Configuration](https://www.mocks-server.org/docs/configuration-options)
1919

2020
## Why a mocks server?
2121

jest.acceptance.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ module.exports = {
66
clearMocks: true,
77

88
testMatch: ["**/test/acceptance/**/?(*.)+(spec|test).js?(x)"],
9-
//testMatch: ["**/test/acceptance/main/programmatic-cli.spec.js"],
109

1110
// Indicates whether the coverage information should be collected while executing the test
1211
collectCoverage: false,

package-lock.json

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
],
2828
"main": "index.js",
2929
"scripts": {
30-
"lint": "eslint index.js lib test jest.config.js jest.acceptance.config.js",
30+
"lint": "eslint index.js src test jest.config.js jest.acceptance.config.js",
3131
"lint-staged": "lint-staged",
3232
"test": "jest",
3333
"test-acceptance": "jest --config=jest.acceptance.config.js --runInBand",
@@ -54,10 +54,12 @@
5454
"prettier": "^1.19.1",
5555
"request": "^2.88.0",
5656
"request-promise": "^4.2.5",
57-
"sinon": "^7.5.0"
57+
"sinon": "^7.5.0",
58+
"strip-ansi": "6.0.0",
59+
"tree-kill-sync": "^1.0.0"
5860
},
5961
"lint-staged": {
60-
"lib/**/*.js": "eslint",
62+
"src/**/*.js": "eslint",
6163
"test/**/*.js": "eslint",
6264
"index.js": "eslint",
6365
"jest.config.js": "eslint",

test/acceptance/CliRunner.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
Copyright 2019 Javier Brea
3+
Copyright 2019 XbyOrange
4+
5+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
10+
*/
11+
12+
"use strict";
13+
14+
const childProcess = require("child_process");
15+
16+
const treeKillSync = require("tree-kill-sync");
17+
const stripAnsi = require("strip-ansi");
18+
19+
const ENCODING_TYPE = "utf8";
20+
21+
module.exports = class CliRunner {
22+
constructor(commandToExecute, options = {}) {
23+
this._command = this.getCommandToExecute(commandToExecute);
24+
this._cwd = options.cwd || process.cwd();
25+
this._logs = [];
26+
27+
this._exitPromise = new Promise(resolve => {
28+
this._resolveExitPromise = resolve;
29+
});
30+
31+
this.logData = this.logData.bind(this);
32+
33+
this.run();
34+
}
35+
36+
getCommandToExecute(command) {
37+
return {
38+
name: command[0],
39+
params: command.splice(1, command.length - 1)
40+
};
41+
}
42+
43+
logData(data) {
44+
const cleanData = stripAnsi(data.replace(/\x1Bc/, ""));
45+
if (cleanData.length) {
46+
this._logs.push(cleanData);
47+
}
48+
}
49+
50+
run() {
51+
if (this._cliProcess) {
52+
throw new Error("Cli is already running");
53+
} else {
54+
this._cliProcess = childProcess.spawn(this._command.name, this._command.params, {
55+
cwd: this._cwd
56+
});
57+
this._cliProcess.stdin.setEncoding(ENCODING_TYPE);
58+
59+
this._cliProcess.stdout.setEncoding(ENCODING_TYPE);
60+
this._cliProcess.stderr.setEncoding(ENCODING_TYPE);
61+
62+
this._cliProcess.stdout.on("data", this.logData);
63+
this._cliProcess.stderr.on("data", this.logData);
64+
65+
this._cliProcess.on("close", code => {
66+
this._exitCode = code;
67+
this._resolveExitPromise(true);
68+
});
69+
}
70+
}
71+
72+
async kill() {
73+
treeKillSync(this._cliProcess.pid);
74+
return this._exitPromise;
75+
}
76+
77+
async hasExit() {
78+
return this._exitPromise;
79+
}
80+
81+
flush() {
82+
this._logs = [];
83+
}
84+
85+
get logs() {
86+
return this._logs.join("");
87+
}
88+
89+
get exitCode() {
90+
return this._exitCode;
91+
}
92+
};

test/acceptance/main/change-behavior.spec.js renamed to test/acceptance/change-behavior.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Unless required by applicable law or agreed to in writing, software distributed
1010

1111
const { startServer, stopServer, request, changeBehavior, getBehaviors } = require("./utils");
1212

13-
describe("web tutorial", () => {
13+
describe("API for changing current behavior", () => {
1414
let server;
1515

1616
beforeAll(async () => {
Lines changed: 19 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,36 @@ Unless required by applicable law or agreed to in writing, software distributed
1111
const path = require("path");
1212
const fsExtra = require("fs-extra");
1313
const {
14-
CliRunner,
1514
request,
1615
changeBehavior,
1716
getBehaviors,
1817
fixturesFolder,
19-
wait
18+
wait,
19+
CliRunner
2020
} = require("./utils");
21-
const InteractiveCliRunner = require("./InteractiveCliRunner");
2221

23-
const runTests = interactiveCli => {
24-
describe("When started", () => {
22+
describe("Plugin listening to core events", () => {
23+
let cli;
24+
25+
beforeAll(async () => {
26+
fsExtra.removeSync(fixturesFolder("files-watch"));
27+
fsExtra.copySync(fixturesFolder("web-tutorial"), fixturesFolder("files-watch"));
28+
cli = new CliRunner(["node", "start-files-watch.js"], {
29+
cwd: path.resolve(__dirname, "fixtures")
30+
});
31+
await wait(1000);
32+
});
33+
34+
afterAll(async () => {
35+
await cli.kill();
36+
});
37+
38+
describe("When server is started", () => {
2539
it("should have 3 behaviors available", async () => {
2640
const behaviors = await getBehaviors();
2741
expect(behaviors.length).toEqual(3);
2842
});
2943

30-
if (interactiveCli) {
31-
it("should display available behaviors in CLI", async () => {
32-
await wait(500);
33-
expect(interactiveCli.cli.logs).toEqual(expect.stringContaining("Behaviors: 3"));
34-
});
35-
it("should display current behavior in CLI", async () => {
36-
expect(interactiveCli.cli.logs).toEqual(
37-
expect.stringContaining("Current behavior: standard")
38-
);
39-
});
40-
}
41-
4244
it("should serve users collection mock under the /api/users path", async () => {
4345
const users = await request("/api/users");
4446
expect(users).toEqual([
@@ -70,12 +72,6 @@ const runTests = interactiveCli => {
7072
expect(behaviors.length).toEqual(4);
7173
});
7274

73-
if (interactiveCli) {
74-
it("should display available behaviors in CLI", async () => {
75-
expect(interactiveCli.cli.logs).toEqual(expect.stringContaining("Behaviors: 4"));
76-
});
77-
}
78-
7975
it("should serve users collection mock under the /api/users path", async () => {
8076
const users = await request("/api/users");
8177
expect(users).toEqual([
@@ -100,15 +96,6 @@ const runTests = interactiveCli => {
10096
await changeBehavior("user2");
10197
});
10298

103-
if (interactiveCli) {
104-
it("should display current behavior in CLI", async () => {
105-
await wait(500);
106-
expect(interactiveCli.cli.logs).toEqual(
107-
expect.stringContaining("Current behavior: user2")
108-
);
109-
});
110-
}
111-
11299
it("should serve users collection mock under the /api/users path", async () => {
113100
const users = await request("/api/users");
114101
expect(users).toEqual([
@@ -133,15 +120,6 @@ const runTests = interactiveCli => {
133120
await changeBehavior("dynamic");
134121
});
135122

136-
if (interactiveCli) {
137-
it("should display current behavior in CLI", async () => {
138-
await wait(500);
139-
expect(interactiveCli.cli.logs).toEqual(
140-
expect.stringContaining("Current behavior: dynamic")
141-
);
142-
});
143-
}
144-
145123
it("should serve users collection mock under the /api/users path", async () => {
146124
const users = await request("/api/users");
147125
expect(users).toEqual([
@@ -166,15 +144,6 @@ const runTests = interactiveCli => {
166144
await changeBehavior("newOne");
167145
});
168146

169-
if (interactiveCli) {
170-
it("should display current behavior in CLI", async () => {
171-
await wait(500);
172-
expect(interactiveCli.cli.logs).toEqual(
173-
expect.stringContaining("Current behavior: newOne")
174-
);
175-
});
176-
}
177-
178147
it("should serve users collection mock under the /api/users path", async () => {
179148
const users = await request("/api/new-users");
180149
expect(users).toEqual([
@@ -194,47 +163,4 @@ const runTests = interactiveCli => {
194163
});
195164
});
196165
});
197-
};
198-
199-
describe("files watcher", () => {
200-
const binaryPath = "../../../../bin/mocks-server";
201-
const cwdPath = path.resolve(__dirname, "fixtures");
202-
const interactiveCli = {
203-
cli: null
204-
};
205-
206-
beforeAll(async () => {
207-
fsExtra.removeSync(fixturesFolder("files-watch"));
208-
fsExtra.copySync(fixturesFolder("web-tutorial"), fixturesFolder("files-watch"));
209-
interactiveCli.cli = new InteractiveCliRunner([binaryPath, "--behaviors=files-watch"], {
210-
cwd: cwdPath
211-
});
212-
await wait();
213-
});
214-
215-
afterAll(async () => {
216-
await interactiveCli.cli.kill();
217-
});
218-
219-
runTests(interactiveCli);
220-
});
221-
222-
describe("files watcher started using Server", () => {
223-
const cwdPath = fixturesFolder("programmatic-server");
224-
let cli;
225-
226-
beforeAll(async () => {
227-
fsExtra.removeSync(fixturesFolder("files-watch"));
228-
fsExtra.copySync(fixturesFolder("web-tutorial"), fixturesFolder("files-watch"));
229-
cli = new CliRunner("start-watch.js", {
230-
cwd: cwdPath
231-
});
232-
await wait();
233-
});
234-
235-
afterAll(async () => {
236-
await cli.kill();
237-
});
238-
239-
runTests();
240166
});

test/acceptance/main/fixtures/files-modification/fixtures/users.js renamed to test/acceptance/fixtures/files-modification/fixtures/users.js

File renamed without changes.

test/acceptance/main/fixtures/files-modification/new-fixtures/users.js renamed to test/acceptance/fixtures/files-modification/new-fixtures/users.js

File renamed without changes.

0 commit comments

Comments
 (0)