1

Error I'm getting Anytime I run npm test:

Dashboard/scripts/tests/connection.test.js:1 ({"Object.":function(module,exports,require,__dirname,__filename,jest){import { DAO } from "../connection.mjs";

SyntaxError: Cannot use import statement outside a module

  at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)

Test Suites: 1 failed, 1 total Tests: 0 total Snapshots: 0 total Time: 0.453 s Ran all test suites. npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! scripts@1.0.0 test: jest npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the scripts@1.0.0 test script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in: npm ERR! /home/manojkumar/.npm/_logs/2021-11-15T15_55_03_917Z-debug.log

My Pakage.json

{
  "name": "scripts",
  "version": "1.0.0",
  "type": "module",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "fs": "0.0.1-security",
    "mysql": "^2.18.1",
    "xml2js": "^0.4.23"
  },
  "devDependencies": {
    "jest": "^27.3.1"
  }
}

connection.test.js

import {DAO} from "../connection.js"
const d=new DAO();

test("Testing Database Connection",()=>{
    expect(d.connect).toBeDefined();
}
);

connection.mjs

import mysql from 'mysql';

export class DAO {
  constructor() {
    this.connection = mysql.createConnection({
      host: "localhost",
      user: "root",
      password: "",
      database: "mydb",
    });
  }

  connect() {
    this.connection.connect((err) => {
      if (err) {
        return console.log(err);
      } else {
        return console.log('MySQL Connected');
      }
    });
  }

  query(sql, args) {
    return new Promise((resolve, reject) => {
        this.connection.query(sql, args, (err, rows) => {
            if (err) {
            return reject(err);
            }
            resolve(rows);
        });
        }
    );
    }
    end(){
      this.connection.end((err)=>{
        if (err) {
          return console.log(err)
        }
        else{
          return console.log("Connection Closed")
        }
      })
    }
}
Manoj Kumar
  • 41
  • 1
  • 6
  • Does this answer your question?https://stackoverflow.com/questions/58211880/uncaught-syntaxerror-cannot-use-import-statement-outside-a-module-when-import – Naveenkumar M Nov 16 '21 at 03:37

0 Answers0