Skip to content

Commit

Permalink
test: add integration test with corejs3 polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Oct 2, 2020
1 parent 7dbe207 commit ed8665b
Show file tree
Hide file tree
Showing 15 changed files with 272 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"devDependencies": {
"@babel/core": "workspace:^7.10.5",
"@babel/helper-plugin-test-runner": "workspace:^7.10.4"
"@babel/helper-plugin-test-runner": "workspace:^7.10.4",
"core-js-pure": "3.6.5"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// prettier-ignore
module.exports = ({ types: t, template }) => ({
visitor: {
Program: {
exit(path) {
path.unshiftContainer(
"body",
template.ast`Symbol.asyncIterator = require("core-js-pure/es/symbol/async-iterator.js");`
);
}
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const log = [];

async function* func1() {
log.push(1);
yield "a";
log.push(2);
}

async function* func2() {
yield* func1();
log.push(3);
}

return (async () => {
const iterator = func2();
await iterator.next();
await iterator.return();

expect(log).toEqual([1]);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"minNodeVersion": "8.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"parserOpts": {
"allowReturnOutsideFunction": true
},
"plugins": [
"transform-async-to-generator",
"proposal-async-generator-functions",
"./inject-corejs-async-iterator-polyfill.js"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const log = [];

async function* inner() {
try {
log.push(1);
yield "a";
log.push(2);
yield "b";
log.push(3);
} finally {
log.push(4);
yield "c";
log.push(5);
}
}

async function* outer() {
log.push(6);
yield* inner();
log.push(7);
}

return (async () => {
const iterator = outer();

let res = await iterator.next();
expect(res).toEqual({ value: "a", done: false });
expect(log).toEqual([6, 1]);

const [res1, res2] = await Promise.all([ iterator.return("x"), iterator.return("y") ]);
expect(res1).toEqual({ value: "c", done: false });
expect(res2).toEqual({ value: "y", done: true });
expect(log).toEqual([6, 1, 4]);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"minNodeVersion": "8.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const log = [];

async function* inner() {
try {
log.push(1);
yield "a";
log.push(2);
yield "b";
log.push(3);
} finally {
log.push(4);
yield "c";
log.push(5);
}
}

async function* outer() {
log.push(6);
yield* inner();
log.push(7);
}

return (async () => {
const iterator = outer();

let res = await iterator.next();
expect(res).toEqual({ value: "a", done: false });
expect(log).toEqual([6, 1]);

res = await iterator.return("x");
expect(res).toEqual({ value: "c", done: false });
expect(log).toEqual([6, 1, 4]);

res = await iterator.return("y");
expect(res).toEqual({ value: "y", done: true });
expect(log).toEqual([6, 1, 4]);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"minNodeVersion": "8.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const log = [];

async function* inner() {
try {
log.push(1);
yield "a";
log.push(2);
yield "b";
log.push(3);
} finally {
log.push(4);
yield "c";
log.push(5);
}
}

async function* outer() {
log.push(6);
yield* inner();
log.push(7);
}

return (async () => {
const iterator = outer();

let res = await iterator.next();
expect(res).toEqual({ value: "a", done: false });
expect(log).toEqual([6, 1]);

res = await iterator.return();
expect(res).toEqual({ value: "c", done: false });
expect(log).toEqual([6, 1, 4]);

res = await iterator.next();
expect(res).toEqual({ value: undefined, done: true });
expect(log).toEqual([6, 1, 4, 5, 7]);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const log = [];

async function* inner() {
log.push(1);
yield "a";
log.push(2);
yield "b";
log.push(3);
}

async function* outer() {
log.push(4);
yield* inner();
log.push(5);
}

return (async () => {
const iterator = outer();

let res = await iterator.next();
expect(res).toEqual({ value: "a", done: false });
expect(log).toEqual([4, 1]);

res = await iterator.return();
expect(res).toEqual({ value: undefined, done: true });
expect(log).toEqual([4, 1]);

res = await iterator.next();
expect(res).toEqual({ value: undefined, done: true });
expect(log).toEqual([4, 1]);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"minNodeVersion": "8.0.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const log = [];

async function* inner() {
try {
log.push(1);
yield "a";
log.push(2);
yield "b";
log.push(3);
} catch (e) {
log.push(4);
yield "c";
log.push(5);
}
}

async function* outer() {
log.push(6);
yield* inner();
log.push(7);
}

return (async () => {
const iterator = outer();

let res = await iterator.next();
expect(res).toEqual({ value: "a", done: false });
expect(log).toEqual([6, 1]);

res = await iterator.throw(new Error("TEST"));
expect(res).toEqual({ value: "c", done: false });
expect(log).toEqual([6, 1, 4]);

res = await iterator.next();
expect(res).toEqual({ value: undefined, done: true });
expect(log).toEqual([6, 1, 4, 5, 7]);
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const log = [];

async function* inner() {
try {
log.push(1);
yield "a";
log.push(2);
yield "b";
log.push(3);
} finally {
log.push(4);
yield "c";
log.push(5);
}
}

async function* outer() {
log.push(6);
yield* inner();
log.push(7);
}

return (async () => {
const iterator = outer();

let res = await iterator.next();
expect(res).toEqual({ value: "a", done: false });
expect(log).toEqual([6, 1]);

res = await iterator.throw(new Error("TEST"));
expect(res).toEqual({ value: "c", done: false });
expect(log).toEqual([6, 1, 4]);

// "yield" in finally suspended the exception for one turn
await expect(iterator.next()).rejects.toThrow(/TEST/);
expect(log).toEqual([6, 1, 4, 5]);
})();
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,7 @@ __metadata:
"@babel/helper-plugin-utils": "workspace:^7.10.4"
"@babel/helper-remap-async-to-generator": "workspace:^7.10.4"
"@babel/plugin-syntax-async-generators": ^7.8.0
core-js-pure: 3.6.5
peerDependencies:
"@babel/core": ^7.0.0-0
languageName: unknown
Expand Down Expand Up @@ -7000,7 +7001,7 @@ __metadata:
languageName: node
linkType: hard

"core-js-pure@npm:^3.0.0, core-js-pure@npm:^3.2.1":
"core-js-pure@npm:3.6.5, core-js-pure@npm:^3.0.0, core-js-pure@npm:^3.2.1":
version: 3.6.5
resolution: "core-js-pure@npm:3.6.5"
checksum: 91fc8e0b699d5bcb11f265ad4544d08c98096b86ad6c9b4c00109616db0aa992ceb58ea82d0dbae2a16658a7aaf2922aa6f9fc1107dc3b0055270799d0414a3f
Expand Down

0 comments on commit ed8665b

Please sign in to comment.