Skip to content

Commit

Permalink
Merge pull request #38 from MattiasBuelens/test-node-support
Browse files Browse the repository at this point in the history
Add unit tests for Node support
  • Loading branch information
MattiasBuelens authored Oct 8, 2019
2 parents 79249c0 + 286a160 commit 808ca16
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 17 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
## Unreleased

* 👓 Align with [spec version `ae5e0cb`](https://github.com/whatwg/streams/tree/ae5e0cb41e9f72cdd97f3a6d47bc674c1f4049d1/) ([#33](https://github.com/MattiasBuelens/web-streams-polyfill/pull/33))
* 💅 Accept polyfilled `AbortSignal`s. ([#36](https://github.com/MattiasBuelens/web-streams-polyfill/pull/36))
* 💅 Polyfill `DOMException` if necessary. ([#37](https://github.com/MattiasBuelens/web-streams-polyfill/pull/37))
* 🐛 Fix support for non-browser environments, such as Node.
* Accept polyfilled `AbortSignal`s. ([#36](https://github.com/MattiasBuelens/web-streams-polyfill/pull/36))
* Polyfill `DOMException` if necessary. ([#37](https://github.com/MattiasBuelens/web-streams-polyfill/pull/37))

## v2.0.4 (2019-08-01)

Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- `npm test` runs the two test suites:
- `npm run test:wpt` runs the [Web Platform Tests for Streams][wpt-streams] against the generated JavaScript bundle, to verify that the polyfill's run-time behavior matches the specification.
- `npm run test:types` runs the TypeScript compiler against some reference code that uses the generated type definitions, to verify that the code successfully passes the type check.
- `npm run test:unit` runs a few unit tests in a Node environment, to verify that the polyfill also works without a browser environment.

## Miscellaneous

Expand Down
10 changes: 0 additions & 10 deletions etc/web-streams-polyfill.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,6 @@ export class CountQueuingStrategy implements QueuingStrategy<any> {
size(): 1;
}

// Warning: (ae-forgotten-export) The symbol "DOMExceptionClass" needs to be exported by the entry point polyfill.d.ts
//
// @public (undocumented)
export type DOMException = DOMExceptionClass;

// Warning: (ae-forgotten-export) The symbol "DOMExceptionConstructor" needs to be exported by the entry point polyfill.d.ts
//
// @public (undocumented)
export const DOMException: DOMExceptionConstructor;

// @public (undocumented)
export interface PipeOptions {
// (undocumented)
Expand Down
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
"module": "dist/polyfill.mjs",
"types": "dist/types/polyfill.d.ts",
"scripts": {
"test": "npm run test:types && npm run test:wpt",
"test": "npm run test:types && npm run test:unit && npm run test:wpt",
"test:wpt": "node --expose_gc ./test/run-web-platform-tests.js",
"pretest:wpt": "git submodule update --init --recursive",
"test:types": "tsc -p ./test/types/tsconfig.json",
"test:unit": "jasmine --config=test/unit/jasmine.json",
"lint": "eslint \"src/**/*.ts\"",
"build": "npm run build:bundle && npm run build:types",
"build:bundle": "rollup -c",
Expand Down Expand Up @@ -50,6 +51,7 @@
"@typescript-eslint/eslint-plugin": "^1.13.0",
"@typescript-eslint/parser": "^1.13.0",
"eslint": "^6.1.0",
"jasmine": "^3.5.0",
"micromatch": "^4.0.2",
"rollup": "^1.18.0",
"rollup-plugin-inject": "^3.0.1",
Expand Down
4 changes: 1 addition & 3 deletions src/ponyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import ByteLengthQueuingStrategy from './lib/byte-length-queuing-strategy';
import CountQueuingStrategy from './lib/count-queuing-strategy';
import { Transformer, TransformStream, TransformStreamDefaultControllerType } from './lib/transform-stream';
import { AbortSignal } from './lib/abort-signal';
import { DOMException } from './stub/dom-exception';

export {
ReadableStream,
Expand Down Expand Up @@ -50,6 +49,5 @@ export {
Transformer,
TransformStreamDefaultControllerType as TransformStreamDefaultController,

AbortSignal,
DOMException
AbortSignal
};
2 changes: 1 addition & 1 deletion src/stub/native.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/// <reference lib="dom" />
export let NativeDOMException: typeof DOMException | undefined = typeof DOMException !== 'undefined' ? DOMException : undefined;
export const NativeDOMException: typeof DOMException | undefined = typeof DOMException !== 'undefined' ? DOMException : undefined;
6 changes: 6 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": ["../.eslintrc.json"],
"rules": {
"@typescript-eslint/no-var-requires": "off"
}
}
6 changes: 6 additions & 0 deletions test/unit/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": ["../.eslintrc.json"],
"env": {
"jasmine": true
}
}
9 changes: 9 additions & 0 deletions test/unit/jasmine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"spec_dir": "test/unit/",
"spec_files": [
"**/*[sS]pec.js"
],
"helpers": [
"helpers/**/*.js"
]
}
57 changes: 57 additions & 0 deletions test/unit/readable-stream/basic.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const { ReadableStream, WritableStream } = require('../../../');
const { FakeAbortSignal } = require('../util/fake-abort-signal');

describe('ReadableStream', () => {
describe('constructor', () => {
it('constructs with no arguments', () => {
const rs = new ReadableStream();
expect(rs instanceof ReadableStream).toBe(true);
});
});

describe('getReader', () => {
it('reads chunks from underlying source', async () => {
const rs = new ReadableStream({
start(c) {
c.enqueue('a');
c.enqueue('b');
c.close();
}
});
const reader = rs.getReader();
expect(await reader.read()).toEqual({ done: false, value: 'a' });
expect(await reader.read()).toEqual({ done: false, value: 'b' });
expect(await reader.read()).toEqual({ done: true, value: undefined });
});
});

describe('pipeTo', () => {
it('accepts an abort signal', async () => {
const rs = new ReadableStream({
start(c) {
c.enqueue('a');
c.close();
}
});
const ws = new WritableStream();
const signal = new FakeAbortSignal(false);
await rs.pipeTo(ws, { signal });
});
it('rejects with an AbortError when aborted', async () => {
const rs = new ReadableStream({
start(c) {
c.enqueue('a');
c.close();
}
});
const ws = new WritableStream();
const signal = new FakeAbortSignal(true);
try {
await rs.pipeTo(ws, { signal });
fail('should have rejected');
} catch (e) {
expect(e.name).toBe('AbortError');
}
});
});
});
17 changes: 17 additions & 0 deletions test/unit/util/fake-abort-signal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class FakeAbortSignal {
constructor(aborted) {
this.aborted = aborted;
}

addEventListener(type, listener) {
return;
}

removeEventListener(type, listener) {
return;
}
}

module.exports = {
FakeAbortSignal
};

0 comments on commit 808ca16

Please sign in to comment.