Skip to content

Commit

Permalink
Fix #22 and #24 and add a failing test case for #23
Browse files Browse the repository at this point in the history
- Do not mix \`async\` and \`done\` usage in jasmine env
- Stop polly instance on \`test_skip\`
- Add a test case for usage of globabl \`Polly.register\`
  (this currently fails on jest-circus)
  • Loading branch information
gribnoysup committed Aug 2, 2021
1 parent 13db62e commit 2c860f1
Show file tree
Hide file tree
Showing 33 changed files with 1,367 additions and 417 deletions.
84 changes: 58 additions & 26 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: Node CI
name: Node.JS CI

on:
push:
branches:
- master
branches:
- main

pull_request:
branches:
- master
branches:
- main

jobs:
build:
Expand All @@ -18,26 +18,58 @@ jobs:

strategy:
matrix:
# jest >= 26 does not support node8 anymore so it is removed from the
# test matrix
# node-version: [8.x, 10.x, 12.x]
node-version: [10.x, 12.x]
node_version: [8, 10, 12, 14]
test_framework: [jasmine@2, jasmine@3, jest@25, jest@latest]
fail-fast: false

steps:
- uses: actions/checkout@v1

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: |
rm -rf node_modules
# We don't have package-lock file in the repo so will be installing
# with good ol `install` and not `ci`
# npm ci
npm install
- name: Run test suite
run: npm run test
- uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node_version }}
uses: actions/setup-node@v2
with:
node_version: ${{ matrix.node_version }}

- name: Install dependencies
run: |
npm install
- name: Install test framework
run: |
npm install ${{ matrix.test_framework }}
- name: Install jest-circus@25
if: |
${{ matrix.test_framework == 'jest@25' }}
run: |
npm install jest-circus@25
- name: Install jest-circus@latest
if: |
${{ matrix.test_framework == 'jest@latest' }}
run: |
npm install jest-circus@latest
- name: Run unit tests
run: |
npm run test:unit
- name: Run jasmine tests
if: |
${{ matrix.test_framework == 'jasmine@2' || matrix.test_framework == 'jasmine@3' }}
run: |
npm run test:jasmine
- name: Run jest-jasmine tests
# jest doesn't support node v8
if: |
${{ (matrix.test_framework == 'jest@25' || matrix.test_framework == 'jest@latest') && matrix.node_version > 8 }}
run: |
npm run test:jest-jasmine2
- name: Run jest-circus tests
# jest doesn't support node v8
if: |
${{ (matrix.test_framework == 'jest@25' || matrix.test_framework == 'jest@latest') && matrix.node_version > 8 }}
run: |
npm run test:jest-circus
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"test:jest-circus": "jest -c ./test/jest-circus-integration.config.json",
"test": "npm run test:unit && npm run test:jest-jasmine2 && npm run test:jest-circus && npm run test:jasmine",
"clear": "rm -rf lib",
"prepare": "npm run clear && npm run build && npm run test",
"prepublishOnly": "npm run prepare"
"prepare": "npm run clear && npm run build",
"prepublishOnly": "npm run test"
},
"files": [
"LICENSE",
Expand Down Expand Up @@ -58,6 +58,5 @@
"repository": {
"type": "git",
"url": "https://github.com/gribnoysup/setup-polly-jest"
},
"dependencies": {}
}
}
46 changes: 28 additions & 18 deletions src/jasmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ function createTestFnProxy(PollyConstructor, fn, jasmineEnv, ctx) {
*/
const { befores, afters } = specHooks.apply(spec, arguments);

const before = async function before(done) {
/**
* NB: Using `done` and `done.fail` so that we support jasmine2 that
* doesn't handle returned promises in before/after and also passing
* `error` to `done` in some versions
*
* @type {JasmineSpec['queueableFn']['fn']} done
*/
const before = function before(done) {
try {
if (globals.isPollyActive) {
const topSuite = jasmineEnv.topSuite();
Expand All @@ -113,25 +120,30 @@ function createTestFnProxy(PollyConstructor, fn, jasmineEnv, ctx) {
);
}

done && done();
done();
} catch (error) {
// If we caught instance of the polly error, we will save it for the
// reference and continue with the tests to print the error at the end
// of the spec where it's more visible
if (error.name === 'PollyError') {
pollyError = error;
done && done();
} else if (done) {
// Otherwise let's just fail spec/throw error, there is nothing
// special we can do in that case
done.fail(error);
done();
} else {
throw error;
// Otherwise let's just fail spec, there is nothing special we can
// do in that case
done.fail(error);
}
}
};

const after = async function after(done) {
/**
* NB: Using `done` and `done.fail` so that we support jasmine2 that
* doesn't handle returned promises in before/after and also passing
* `error` to `done` in some versions
*
* @type {JasmineSpec['queueableFn']['fn']} done
*/
const after = function after(done) {
try {
// We want to throw polly error here so it's shown as the last one in the
// list of possible errors that happend during the test run
Expand All @@ -144,17 +156,15 @@ function createTestFnProxy(PollyConstructor, fn, jasmineEnv, ctx) {
}

if (globals.pollyContext.polly) {
await globals.pollyContext.polly.stop();
globals.pollyContext.polly = null;
}

done && done();
} catch (error) {
if (done) {
done.fail(error);
globals.pollyContext.polly.stop().then(() => {
globals.pollyContext.polly = null;
done();
});
} else {
throw error;
done();
}
} catch (error) {
done.fail(error);
}
};

Expand Down
16 changes: 13 additions & 3 deletions src/jest-environment-polly.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@ export function PollyEnvironmentFactory(
}

/**
*
* @param {CircusEvent} event
* @param {CircusState} state
*/
// TS types demand that this is an instance property, not a prototype method
// @ts-expect-error
async handleTestEvent(event, state) {
if (super.handleTestEvent) {
// TS is confused because we are explicitly awaiting (to cover both
// sync and async handlers) but it really wants us to handle only one
// of those
// @ts-expect-error
await super.handleTestEvent(event, state);
}

Expand All @@ -81,8 +86,13 @@ export function PollyEnvironmentFactory(
);
}

// Stop Polly instance if there is one running
if (event.name === 'test_done' && this.pollyGlobals.pollyContext.polly) {
// Stop Polly instance if there is one running. We check `skip` in
// addition to done because specs emit `test_start` even when they will be
// then skipped
if (
['test_done', 'test_skip'].includes(event.name) &&
this.pollyGlobals.pollyContext.polly
) {
await this.pollyGlobals.pollyContext.polly.stop();
this.pollyGlobals.pollyContext.polly = null;
}
Expand Down
11 changes: 11 additions & 0 deletions test/getPost.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fetch = require('node-fetch');

const getPost = async id => {
const response = await fetch(
`https://jsonplaceholder.typicode.com/posts/${id}`
);
const json = await response.json();
return Object.assign(json, { ok: response.ok, status: response.status });
};

module.exports = { getPost };
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"creator": {
"comment": "persister:fs",
"name": "Polly.JS",
"version": "2.6.2"
"version": "5.1.1"
},
"entries": [
{
Expand Down Expand Up @@ -53,20 +53,11 @@
"size": 2,
"text": "{}"
},
"cookies": [
{
"domain": ".typicode.com",
"expires": "2020-09-13T08:23:15.000Z",
"httpOnly": true,
"name": "__cfduid",
"path": "/",
"value": "d140d7b80dde6e698ca271bfcebfde0891568449395"
}
],
"cookies": [],
"headers": [
{
"name": "date",
"value": "Sat, 14 Sep 2019 08:23:15 GMT"
"value": "Mon, 02 Aug 2021 08:39:34 GMT"
},
{
"name": "content-type",
Expand All @@ -80,15 +71,22 @@
"name": "connection",
"value": "close"
},
{
"_fromType": "array",
"name": "set-cookie",
"value": "__cfduid=d140d7b80dde6e698ca271bfcebfde0891568449395; expires=Sun, 13-Sep-20 08:23:15 GMT; path=/; domain=.typicode.com; HttpOnly"
},
{
"name": "x-powered-by",
"value": "Express"
},
{
"name": "x-ratelimit-limit",
"value": "1000"
},
{
"name": "x-ratelimit-remaining",
"value": "998"
},
{
"name": "x-ratelimit-reset",
"value": "1627893596"
},
{
"name": "vary",
"value": "Origin, Accept-Encoding"
Expand All @@ -99,15 +97,15 @@
},
{
"name": "cache-control",
"value": "public, max-age=14400"
"value": "max-age=43200"
},
{
"name": "pragma",
"value": "no-cache"
},
{
"name": "expires",
"value": "Sat, 14 Sep 2019 12:23:15 GMT"
"value": "-1"
},
{
"name": "x-content-type-options",
Expand All @@ -123,41 +121,49 @@
},
{
"name": "cf-cache-status",
"value": "HIT"
},
{
"name": "age",
"value": "0"
"value": "MISS"
},
{
"name": "expect-ct",
"value": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""
},
{
"name": "report-to",
"value": "{\"endpoints\":[{\"url\":\"https:\\/\\/a.nel.cloudflare.com\\/report\\/v3?s=08L4z7aOfC07Ed3dUVCfbiKoLEzbyzeTbw71EjRpfLxsmQA6yk1NhgGnOWaJalzwlOyGAzGC8MFvoOH564CAB5ExwtB5UOJnrQr6SVuAsBeZrfP5zsuNsu2ya%2FvUboNVguPzMj82Xb%2BnSIVwI%2FEw\"}],\"group\":\"cf-nel\",\"max_age\":604800}"
},
{
"name": "nel",
"value": "{\"report_to\":\"cf-nel\",\"max_age\":604800}"
},
{
"name": "server",
"value": "cloudflare"
},
{
"name": "cf-ray",
"value": "5160f533cca2c29f-FRA"
"value": "6785ff166d24d463-HAM"
},
{
"name": "alt-svc",
"value": "h3-27=\":443\"; ma=86400, h3-28=\":443\"; ma=86400, h3-29=\":443\"; ma=86400, h3=\":443\"; ma=86400"
}
],
"headersSize": 727,
"headersSize": 1046,
"httpVersion": "HTTP/1.1",
"redirectURL": "",
"status": 404,
"statusText": "Not Found"
},
"startedDateTime": "2019-09-14T08:23:15.735Z",
"time": 92,
"startedDateTime": "2021-08-02T08:38:14.563Z",
"time": 318,
"timings": {
"blocked": -1,
"connect": -1,
"dns": -1,
"receive": 0,
"send": 0,
"ssl": -1,
"wait": 92
"wait": 318
}
}
],
Expand Down
Loading

0 comments on commit 2c860f1

Please sign in to comment.