modernweb-dev / web

Guides, tools and libraries for modern web development.
https://modern-web.dev
MIT License
2.22k stars 291 forks source link

Not able to run tests if test files are not inside src directory using Snowpack #1446

Open ShivamJoker opened 3 years ago

ShivamJoker commented 3 years ago
tests/App.test.jsx:

 🚧 Browser logs:
      TypeError: Failed to fetch dynamically imported module: http://localhost:8000/easy-collab-frontend/tests/App.test.jsx?wtr-session-id=b3ca1529-8150-4d2c-b050-5a62560ca4c0

When I put my whole tests folder inside the src directory everything seems to work but if I put it in root it starts giving the error.

// App.test.jsx

import React from 'react';
import { render, screen } from '@testing-library/react';
import { expect } from 'chai';

import '../src/internationalization/index';
import App from '../src/components/App';

describe('App', () => {
  it('renders App component', () => {
    render(<App />);
    const unauth = screen.getByText('Unauthorized Access!!');
    expect(document.body.contains(unauth));
  });
});
//  web-test-runner.config.js

process.env.NODE_ENV = 'test';
const str = require('@snowpack/web-test-runner-plugin')();

module.exports = {
  testFramework: {
    config: {
      ui: 'bdd',
      timeout: '2000',
    },
  },
  plugins: [str],
  coverageConfig: {
    exclude: ['**/*/_snowpack/**/*'],
  },
};

Script

    "test": "web-test-runner \"tests/**/*.test.jsx\" --node-resolve",

If someone can help, it would be great or if it's a bug then please provide some workarounds.

Westbrook commented 3 years ago

@ShivamJoker are you able to share a repo with a recreation of this situation?

I'm not acquainted with @snowpack/web-test-runner-plugin and whether the build tooling that this binds itself to requires the code to be colocated to build the files under test in this case. Have you discussed this with anyone at https://discord.com/invite/snowpack to be sure you're consuming that plugin as expected?

ShivamJoker commented 3 years ago

@Westbrook here is the repo link https://gitlab.com/realdevexp/easy-collab-frontend/-/tree/15-fix/api-config The branch is 15-fix/api-config

I have looked into the docs and examples many times to check if everything is being configured properly or not. If you have any other way to run tests without using the @snowpack/web-test-runner-plugin, please let me know.

ShivamJoker commented 3 years ago

And even if I remove the whole config file I still get the same error 😢

IanVS commented 3 years ago

I noticed this as well, and I solved it by checking in my snowpack.config.js if process.env.NODE_ENV is "test", and if so, I add a mount for the test files.

ShivamJoker commented 3 years ago

@IanVS can you please share the minimal config to get it working ?

IanVS commented 3 years ago

@ShivamJoker here's an example with most of the rest of the config stripped out:

const IS_TESTING = process.env.NODE_ENV === 'test';

const mount = {
  public: { url: '/', static: true },
  src: { url: '/dist' },
};

if (IS_TESTING) {
  mount['test-setup'] = { url: '/test-setup' };
}

module.exports = {
  mount,
  plugins: [],
  routes: [],
  optimize: {},
  packageOptions: {
    polyfillNode: IS_TESTING,
  },
  devOptions: {},
  buildOptions: {},
};

In my case, I put my actual test files next to my components in /src, but I have some setup files that I only want to mount when I'm testing. You could do something similar for your /tests directory.

ShivamJoker commented 3 years ago

Hey thanks @IanVS it works. But I'll leave this open because still it should be implemented by library.

Labham-Jain commented 3 years ago

Thanks, it worked!

IanVS commented 3 years ago

@ShivamJoker what would you suggest should be implemented?