shopware / jest-preset-sw6-admin

Jest Test preset for Shopware 6 administration unit tests
MIT License
4 stars 2 forks source link

Could not locate module src/core/factory/module.factory mapped as: undefined/src$1 #12

Open moehrenzahn opened 4 years ago

moehrenzahn commented 4 years ago

Hey everyone, I'm trying to run administration jest tests for my SW6 plugin located at custom/plugins.

I am running into the following error when running a test via jest:

cd src/Resources/app/administration/ && npm run test
Test suite failed to run

    Configuration error:

    Could not locate module src/core/factory/module.factory mapped as:
    undefined/src$1.

    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^src(.*)$/": "undefined/src$1"
      },
      "resolver": undefined
    }

       6 | const Bottle = require('bottlejs');
       7 | 
    >  8 | const ModuleFactory = require('src/core/factory/module.factory').default;
         |                       ^
       9 | const ComponentFactory = require('src/core/factory/component.factory').default;
      10 | const TemplateFactory = require('src/core/factory/template.factory').default;
      11 | const EntityFactory = require('src/core/factory/entity.factory').default;

      at createNoMappedModuleFoundError (node_modules/jest-resolve/build/index.js:552:17)
      at Object.<anonymous> (../../../../../../../vendor/shopware/platform/src/Administration/Resources/app/administration/src/core/shopware.js:8:23)

This is my package.json:

{
    "name": "my-plugin-administation",
    "version": "1.0.0",
    "description": "",
    "scripts": {
        "test": "jest"
    },
    "author": "",
    "license": "MIT",
    "dependencies": {},
    "devDependencies": {
        "@babel/plugin-proposal-class-properties": "^7.10.4",
        "@babel/preset-env": "^7.11.0",
        "@shopware-ag/jest-preset-sw6-admin": "^1.1.2-beta.1",
        "jest": "^26.4.0"
    }
}

And my jest.config.js:

module.exports = {
    preset: '@shopware-ag/jest-preset-sw6-admin',
    globals: {
        adminPath: '../../../../../../../vendor/shopware/platform/src/Administration/Resources/app/administration'
    }
};

And my babel.config.json:

{
    "presets": ["@babel/preset-env"],
    "plugins": ["@babel/plugin-proposal-class-properties"]
}

Any help would be appreciated, I also wouldn't rule out that I'm doing something wrong!

moehrenzahn commented 4 years ago

I can get it to work when adding the ADMIN_PATH env variable myself to the environment:

(cd src/Resources/app/administration/ && npm install && ADMIN_PATH='/myProject/vendor/shopware/platform/src/Administration/Resources/app/administration' npm test)

This works. Using a relative ADMIN_PATH does not work. Putting the full path into jest.config.js also does not work.

klarstil commented 4 years ago

Thanks for the information, I'm taking care of it 👍

rammi22 commented 4 years ago

I can get it to work when adding the ADMIN_PATH env variable myself to the environment:

(cd src/Resources/app/administration/ && npm install && ADMIN_PATH='/myProject/vendor/shopware/platform/src/Administration/Resources/app/administration' npm test)

This works. Using a relative ADMIN_PATH does not work. Putting the full path into jest.config.js also does not work.

Can you provide the full working configuration?

I try to execute the test from the cli, but it does retrun the same error rammi@rammi-Akoya-P5105-D-MD8856-2414:~/My/Local/Path/custom/plugins/MyPlugin/src/Resources/app/administration$ (npm install && ADMIN_PATH='/My/Local/Path/vendor/shopware/platform/src/Administration/Resources/app/administration' npm test)

returns error

...
    Configuration error:

    Could not locate module src/core/factory/module.factory mapped as:
    /My/Local/Path/custom/plugins/MyPlugin/src/Resources/app/administration/src$1.

    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^src(.*)$/": "/My/Local/Path/custom/plugins/MyPlugin/src/Resources/app/administration/src$1"
      },
      "resolver": undefined
    }
// jest.config.js

module.exports = {
    preset: '@shopware-ag/jest-preset-sw6-admin',
    globals: {
        adminPath: '../../../../../../../vendor/shopware/platform/src/Administration/Resources/app/administration'
    }
}
// babel.config.json

{
    "presets": ["@babel/preset-env"],
    "plugins": ["@babel/plugin-proposal-class-properties"]
}
rammi22 commented 4 years ago

With following steps it works for me:

// package.json

...
  "devDependencies": {
        "@babel/plugin-proposal-class-properties": "^7.10.4",
        "@babel/preset-env": "^7.11.5",
        "@shopware-ag/jest-preset-sw6-admin": "^1.1.1",
        "@vue/test-utils": "^1.1.0",
        "babel-plugin-module-resolver": "^4.0.0",
        "jest": "^26.4.2",
        "vue-jest": "^3.0.7"
    },
    "dependencies": {
        "test": "^0.6.0",
        "vue": "^2.6.12",
        "vue-template-compiler": "^2.6.12"
    }
...
// jest.config.js

module.exports = {
    preset: '@shopware-ag/jest-preset-sw6-admin',
    globals: {
        // required, e.g. /www/sw6/platform/src/Administration/Resources/app/administration
        adminPath: "/Project/vendor/shopware/platform/src/Administration/Resources/app/administration"
    },
    moduleNameMapper: {
        '^src(.*)$': '/Project/vendor/shopware/platform/src/Administration/Resources/app/administration/src$1'
    },
}
// babel.config.json
{
    "presets": ["@babel/preset-env"],
    "plugins": [
        "@babel/plugin-proposal-class-properties",
        ["module-resolver", {
            "root": ["./src"],
            "alias": {
                "/src": "./src"
            }
        }]
    ]
}
// index.spec.js (the test file)

import { shallowMount } from "@vue/test-utils";
import '/src/module/myplugin/view/mycomponent';

describe('view/mycomponent', () => {
    let wrapper;

    beforeEach(() => {
        wrapper = shallowMount(Shopware.Component.build('mycomponent'));
    });

    afterEach(() => {
        wrapper.destroy();
    });

    it('should be a Vue.js component', () => {
        expect(wrapper.isVueInstance()).toBeTruthy();
    })
})
rammi22 commented 1 year ago

Solution https://stackoverflow.com/a/75571063/2702011