mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
Node 6 support (#484)
This patch: - introduces a transpiler which substitutes async/await logic with generators. - starts using the transpiler to generate a node6-compatible version of puppeteer - introduces a runtime-check to decide which version of code to use Fixes #316.
This commit is contained in:
committed by
Andrey Lushnikov
parent
46115f9182
commit
9212863b92
67
utils/node6-transform/test/test.js
Normal file
67
utils/node6-transform/test/test.js
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Copyright 2017 Google Inc. All rights reserved.
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
const transformAsyncFunctions = require('../TransformAsyncFunctions');
|
||||
|
||||
describe('TransformAsyncFunctions', function() {
|
||||
it('should convert a function expression', function(done) {
|
||||
const input = `(async function(){ return 123 })()`;
|
||||
const output = eval(transformAsyncFunctions(input));
|
||||
expect(output instanceof Promise).toBe(true);
|
||||
output.then(result => expect(result).toBe(123)).then(done);
|
||||
});
|
||||
it('should convert an arrow function', function(done) {
|
||||
const input = `(async () => 123)()`;
|
||||
const output = eval(transformAsyncFunctions(input));
|
||||
expect(output instanceof Promise).toBe(true);
|
||||
output.then(result => expect(result).toBe(123)).then(done);
|
||||
});
|
||||
it('should convert an arrow function with curly braces', function(done) {
|
||||
const input = `(async () => { return 123 })()`;
|
||||
const output = eval(transformAsyncFunctions(input));
|
||||
expect(output instanceof Promise).toBe(true);
|
||||
output.then(result => expect(result).toBe(123)).then(done);
|
||||
});
|
||||
it('should convert a function declaration', function(done) {
|
||||
const input = `async function f(){ return 123; } f();`;
|
||||
const output = eval(transformAsyncFunctions(input));
|
||||
expect(output instanceof Promise).toBe(true);
|
||||
output.then(result => expect(result).toBe(123)).then(done);
|
||||
});
|
||||
it('should convert await', function(done) {
|
||||
const input = `async function f(){ return 23 + await Promise.resolve(100); } f();`;
|
||||
const output = eval(transformAsyncFunctions(input));
|
||||
expect(output instanceof Promise).toBe(true);
|
||||
output.then(result => expect(result).toBe(123)).then(done);
|
||||
});
|
||||
it('should convert method', function(done) {
|
||||
const input = `class X{async f() { return 123 }} (new X()).f();`;
|
||||
const output = eval(transformAsyncFunctions(input));
|
||||
expect(output instanceof Promise).toBe(true);
|
||||
output.then(result => expect(result).toBe(123)).then(done);
|
||||
});
|
||||
it('should pass arguments', function(done) {
|
||||
const input = `(async function(a, b){ return await a + await b })(Promise.resolve(100), 23)`;
|
||||
const output = eval(transformAsyncFunctions(input));
|
||||
expect(output instanceof Promise).toBe(true);
|
||||
output.then(result => expect(result).toBe(123)).then(done);
|
||||
});
|
||||
it('should still work across eval', function(done) {
|
||||
const input = `var str = (async function(){ return 123; }).toString(); eval('(' + str + ')')();`;
|
||||
const output = eval(transformAsyncFunctions(input));
|
||||
expect(output instanceof Promise).toBe(true);
|
||||
output.then(result => expect(result).toBe(123)).then(done);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user