Skip to content

Commit

Permalink
test: await all promises in upload test (#2557)
Browse files Browse the repository at this point in the history
Otherwise, we get unhandled promise rejection sometimes.
  • Loading branch information
dgozman authored Jun 12, 2020
1 parent 1bf9e65 commit defeeb9
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions test/input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,26 +135,12 @@ describe('Page.waitForFileChooser', function() {
expect(await page.$eval('input', input => input.files[0].name)).toBe('file-to-upload.txt');
});
it('should detect mime type', async({page, server}) => {
let callback;
const result = new Promise(f => callback = f);
let files;
server.setRoute('/upload', async (req, res) => {
const form = new formidable.IncomingForm();
form.parse(req, function(err, fields, { file1, file2 }) {
expect(file1.name).toBe('file-to-upload.txt');
expect(file1.type).toBe('text/plain');
expect(
fs.readFileSync(file1.path).toString()
).toBe(
fs.readFileSync(path.join(__dirname, '/assets/file-to-upload.txt')).toString()
);
expect(file2.name).toBe('pptr.png');
expect(file2.type).toBe('image/png');
expect(
fs.readFileSync(file2.path).toString()
).toBe(
fs.readFileSync(path.join(__dirname, '/assets/pptr.png')).toString()
);
callback();
form.parse(req, function(err, fields, f) {
files = f;
res.end();
});
});
await page.goto(server.EMPTY_PAGE);
Expand All @@ -166,8 +152,19 @@ describe('Page.waitForFileChooser', function() {
</form>`)
await (await page.$('input[name=file1]')).setInputFiles(path.join(__dirname, '/assets/file-to-upload.txt'));
await (await page.$('input[name=file2]')).setInputFiles(path.join(__dirname, '/assets/pptr.png'));
page.click('input[type=submit]');
await result;
await Promise.all([
page.click('input[type=submit]'),
server.waitForRequest('/upload'),
]);
const { file1, file2 } = files;
expect(file1.name).toBe('file-to-upload.txt');
expect(file1.type).toBe('text/plain');
expect(fs.readFileSync(file1.path).toString()).toBe(
fs.readFileSync(path.join(__dirname, '/assets/file-to-upload.txt')).toString());
expect(file2.name).toBe('pptr.png');
expect(file2.type).toBe('image/png');
expect(fs.readFileSync(file2.path).toString()).toBe(
fs.readFileSync(path.join(__dirname, '/assets/pptr.png')).toString());
});
it('should be able to read selected file', async({page, server}) => {
await page.setContent(`<input type=file>`);
Expand Down

0 comments on commit defeeb9

Please sign in to comment.