Issue with Jest tests in Next.js

I seem to be hitting an error with Jest tests when using DrizzleORM in my tests. I get
Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Jest encountered an unexpected token

Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
And then
/Users/a.laycock/Desktop/Work/project/database/client.ts:18
const connection = await _promise.default.createConnection({
^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules
/Users/a.laycock/Desktop/Work/project/database/client.ts:18
const connection = await _promise.default.createConnection({
^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules
6 Replies
Angelelz
Angelelz17mo ago
The error is self explanatory, you can't use await outside an async function or top level in an ESM module. You need to a) use callback syntax, b) wrap all your code in an async function and call it at the end or c) use the callback createConnection if you driver provides it.
Aidan Laycock
Aidan LaycockOP17mo ago
Hey @angelelz, I'm not quite sure what you mean! I've just copied the client connection from the documentation
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";

const connection = await mysql.createConnection({
host: "host",
user: "user",
database: "database",
...
});

const db = drizzle(connection);
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";

const connection = await mysql.createConnection({
host: "host",
user: "user",
database: "database",
...
});

const db = drizzle(connection);
Which is what's throwing the error in a client.ts file.
Angelelz
Angelelz17mo ago
Try including
"type": "module"
"type": "module"
in your package.json and run it with
$ ts-node --esm drizzle/migration.ts
$ ts-node --esm drizzle/migration.ts
Angelelz
Angelelz17mo ago
DEV Community
Implicit ESM in Node.js with "type": "module"
A brief article on the usage of "type": "module" in package.json to have .js files parsed as ECMAScript Modules (ESM).
Aidan Laycock
Aidan LaycockOP17mo ago
Hey @angelelz - Thanks for the help with this so far! I have tried the Module update, but it hasn't resolved it, as I'm trying to access the Client file through Jest, not directly to just run migrations (I'm wanting to generate some Integration tests across a few services we're overhauling from Prisma to Drizzle). Any further help would be greatly appreciated For anyone coming to this in the future, I've resolved this by doing:
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2";

async function main() {
}

const connection = mysql.createConnection({
host: process.env.DB_URL,
database: process.env.DB_NAME,
... settings,
});

export const db = drizzle(connection.promise());
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2";

async function main() {
}

const connection = mysql.createConnection({
host: process.env.DB_URL,
database: process.env.DB_NAME,
... settings,
});

export const db = drizzle(connection.promise());
Usman
Usman4mo ago
Did you face any future problems or is it better to use createPool instead
No description
Want results from more Discord servers?
Add your server