0

Using Playwright, I have this dummy test:

import { test, expect, chromium } from '@playwright/test';
import * as path from "path";
import fs from "fs-extra";
const INPUT_DIR = path.resolve(__dirname, "./../data");
const items1 = JSON.stringify((await fs.readJson(path.resolve(INPUT_DIR, "items1.json"))));
const expected1 = '0.999999111111';

test("test1", async () => {
  const browser = await chromium.launch({ headless: false });
  const context = await browser.newContext();
  const page = await context.newPage();
  await page.goto('http://localhost:3000');
  expect('abc').toContain('a'); // <-- dummy test
});

I run the test with:

npx playwright test readJsonAsyncProblem.spec.ts

I get (exact and full output):

SyntaxError: missing ) after argument list
Error: No tests found

I've faced the same problem using "await csvtojson" in other playwright tests.

If I change line #5 to:

const items1 = JSON.stringify((fs.readJsonSync(path.resolve(INPUT_DIR, "items1.json"))));

the test passes.

I could not figure out why using await readJson causes this problem. My experience is limited in Playwright. Help is appreciated.

kal
  • 41
  • 2
  • 5
  • 3
    This could be happening if a) your node.js version is too low and/or b) this code is being transpiled to cjs (top level await is not available except in ESM in node). But really it doesn't matter, just [read the file synchronously](https://nodejs.org/api/fs.html#fsreadfilesyncpath-options) and avoid the whole issue. – Jared Smith May 23 '23 at 14:39
  • Thank Jared. I think that is the case. The await at top level. Why though is the error a syntax one? – kal May 24 '23 at 12:23

0 Answers0