dimanche 10 janvier 2021

Teseting NodeJS with Mocha: 'Require is not defined'

EDIT: If you're going to downvote, please at least indicate why. If this is a frequently-asked question, I'd welcome a link to the answer. As said, I did look, but couldn't find it.


I have a very simple Node file that I'm trying to test with Mocha/Chai. But when I run the Mocha test, I get the error ERROR: ReferenceError: require is not defined `

I did some googling for people experiencing the same problem but the examples that I came up with were when they were running the test in the browser (see, for example, Mocha, "require is not defined" when test in browser).

The file I want to test, index.js


const argv = require('minimist')(process.argv.slice(2));


const digitTester = /\d/g;
const capTester = /[A-Z]/g;

const dict  = {
    length:10,
    must_have_numbers: true,
    must_have_caps: true
}

export default function passwordCheck(password) {
    if (!password) return false;
    if (typeof password !== "string") return false;
    if (password.length < dict.length) return false; // assumes that 10 is a MINIMUM length
    if (dict.must_have_numbers && !digitTester.test(password)) return false;
    return !(dict.must_have_caps && !capTester.test(password));

}
if (argv._.length) {
   console.log(passwordCheck(argv._[0]))
}

/**
 * alternate version to check a lot of passwords:
 *
 * if (argv._.length) {
 *  for (const pwd of argv_) console.log(passwordCheck(pwd)
 *}
 *
 */


the mocha file, test/index.test.js



const chai = require('chai')
const expect = chai.expect

const passwordCheck = require('../index.js')

const tooShort = "A2a"
const noCaps = "a2abcdefghijklmnop"
const noNumber = "Aabcdefghijklmnop"
const good = "A2abcdefghijklmnop"

describe('password checking', () => {
  it('should return false for passwords less than length 10', () => {
    expect(passwordCheck(tooShort)).to.be.false;
  });
  it('should return false for passwords without a capital letter', () => {
    expect(passwordCheck(noCaps)).to.be.false;
  });
  it('should return false for passwords without a number', () => {
    expect(passwordCheck(noNumber)).to.be.false;
  });
  it('should return true for passwords that match criteria', () => {
    expect(passwordCheck(good)).to.be.true;
  });

});

and package.json

{
  "name": "codetest",
  "version": "1.0.0",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "mocha"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "@types/minimist": "^1.2.1",
    "@types/node": "^14.14.20",
    "chai": "^4.2.0",
    "minimist": "^1.2.5",
    "mocha": "^8.2.1"
  }
}

and the error message is

✖ ERROR: ReferenceError: require is not defined
    at file:///Users/robertanthony/Documents/Projects/sandbox/pwd_checker/index.js:2:14
    at ModuleJob.run (node:internal/modules/esm/module_job:152:23)
    at async Loader.import (node:internal/modules/esm/loader:166:24)
    at async exports.handleRequires (/Users/robertanthony/Documents/Projects/sandbox/pwd_checker/node_modules/mocha/lib/cli/run-helpers.js:94:28)
    at async /Users/robertanthony/Documents/Projects/sandbox/pwd_checker/node_modules/mocha/lib/cli/run.js:341:25
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Node 15.

I'd welcome any help.

Aucun commentaire:

Enregistrer un commentaire