lecture 14 of 150 completed

Modules: import and export

The first line of a test file is an import. Helpers and page objects are shared between files the same way.

What you'll learn

  • What export and import do
  • The difference between a named export and a default export
  • How to read the two import errors you will see most

You move the lateFee helper into its own file so that several tests can use it. The test file stops before a single test runs:

SyntaxError: The requested module './fees.js' does not provide an export named 'latefee'

This is the wording Node and Chrome use. Firefox and Safari report the same problem in other words.

Each JavaScript file is a module. A module keeps its names to itself. Another file can only use what the module chooses to share.

export shares a name

Here is fees.js:

export const DAILY_FEE = 0.5;

export function lateFee(daysLate) {
  return daysLate * DAILY_FEE;
}

function roundToCents(amount) {
  return Math.round(amount * 100) / 100;
}

export in front of a function or a variable shares it. lateFee and DAILY_FEE are shared. roundToCents has no export, so no other file can use it.

import takes a name

Here is the test file, in the same folder:

import { lateFee, DAILY_FEE } from './fees.js';

console.log(lateFee(4));
console.log(DAILY_FEE);
  1. The braces list the names you want. They must match the exported names exactly, capital letters included.
  2. './fees.js' is the path to the file. ./ means "the folder this file is in". ../ means one folder up.

The error at the top came from import { latefee }, with a small f. The module exports lateFee. Node checks every imported name before it runs any code, so one wrong letter stops the whole file.

Tell Node the file uses import

Node has two module styles, so it needs to know which one a .js file uses. Put this line in the package.json file of the project:

{ "type": "module" }

Without it, older versions of Node stop at the first import with "Cannot use import statement outside a module". Playwright and Cypress projects handle this for their own test files.

Names from a package

The first line of a Playwright test has no ./ in its path:

import { test, expect } from '@playwright/test';

A path that does not start with ./ or ../ is the name of an installed package. Node looks for it in the node_modules folder. The braces work the same way. test and expect are two names that the package exports.

The default export

A module can mark one thing as its default export. You import a default with no braces, and you may give it any name.

export default class LoansPage {
  // a page object for the loans page
}
import LoansPage from './loans-page.js';

A page object is a class that holds the selectors and actions for one page. Many teams write one per file and export it as the default.

Mixing up the two kinds gives the second error you will see often. With import lateFee from './fees.js', Node says the module does not provide an export named 'default'. The fix is to add the braces, because lateFee is a named export.

The older style

Older test suites share code with require and module.exports. This is the CommonJS style, which Node used before import existed.

const { lateFee } = require('./fees.js');

It does the same job. You will meet both in real projects, so learn to recognise each. Do not mix them in one file.

Why this matters on the job

A helper that is copied into ten test files gets fixed in one of them. Put it in a module and import it, so one fix reaches every test. When an import fails, no test in that file runs, and a CI report can show the whole file as one failure. Read the first error in the log before you look at any test.

Remember this

export shares a name, and import { name } takes it by that exact name. A default export is imported without braces. A path that starts with ./ is a file, and a bare name is a package.

Reference: JavaScript modules in the MDN JavaScript Guide.

now prove it

Reading is the setup. Solve this exercise in the editor. We run your code on each case and compare what it prints with the expected output.