As a JavaScript newbie tasked with maintaining a large application, I recently implemented a new feature that passed manual testing successfully in both development and production modes. However, when running tests using vue-cli-service test:unit
(mocha + chai), I encountered a runtime exception:
Exception occurred while loading your tests`
ReferenceError: Cannot access 'serviceBase' before initialization
The issue seems to stem from the serviceBase.json
file:
import api from "./api";
import store from "./../store";
import i18n from "../../plugins/i18n";
export const serviceBase = {
api: api,
resource: "",
preList: "me",
preListOption: {
getList: true
},
{.....}
This serviceBase
is utilized in various areas, including the documentImageService.js
file:
import { createErrorMessage, serviceBase, clearance } from "../service";
import api from "../api";
import axios from "axios";
const DocumentImage = {
...serviceBase,
clearance,
{.....}
The DocumentImage
constant is then imported in the renderer.js
file:
import * as HB from "handlebars";
import { Helpers } from "@lextira/lexgate-document-renderer";
import DocumentImage from "@/store/utils/services/documentImageService.js";
export default class {
/** @type {HB} */
handlebars = undefined;
/** @type {string} */
clearanceToken = undefined;
{.....}
Removing the import of DocumentImage
allows the tests to run without issues, indicating that the test runtime is being affected. While the feature works fine in the browser runtime, the test environment seems to be reacting differently.
I suspect the problem lies in the mocha runtime configuration, but I haven't found any relevant information in the current documentation to address this issue.