DevExpress / testcafe-examples

Ready-to-run examples for TestCafe
https://testcafe.io
MIT License
50 stars 53 forks source link

mock date example does not work with mockdate v3 #49

Closed theinterned closed 3 years ago

theinterned commented 3 years ago

Thanks you for the great recipe for mocking dates https://github.com/DevExpress/testcafe-examples/tree/master/examples/mock-date

I was able to get it to work using mockdate@v2.0.5. But the subsequent mockdate@v3x breaks.

mockdate@v3x moves the package to typescript and it looks like the build target has changed from UMD to CJS.

Attempting to load this CJS module via clientScripts({ module: 'mockdate' }) (as in the example) results in an error:

1) - Error in fixture.beforeEach hook -
      An error occurred in the 'mockdate' module injected into the tested page.
      Make sure that this module can be executed in the browser environment.

      Error details:
      ReferenceError: exports is not defined

If there is a way to wrap this CJS module so that the mock-date example continues to work please let me know and I'd be happy to open a PR to update the example.

Otherwise, I would be happy to open a PR to update the README to specify using mockdate@v2.

Dmitry-Ostashev commented 3 years ago

As mentioned in the documentation, a browser must be able to execute the injected module. CommonJS modules cannot be executed without additional tools (e.g. RequireJS). We don't mind your PR to update the README file.

theinterned commented 3 years ago

Yes thank you @Dmitry-Ostashev i realize the issue is documented elsewhere. My comment is that this example is broken as it is currently implemented.

Are you suggesting the solution should be to use require.js To load the mockdate Module? How would that look?

Thank you.

Dmitry-Ostashev commented 3 years ago

@theinterned The mockdate module version ^2.0.5 is specified in the package.json file, and the example works correctly. You can check this if you clone the repository and run this test. The problem is only related to mockdate@3.x. The mockdate version 2.x. works correctly. So, you can use the mockdate@2.x version or wait until this issue is fixed: https://github.com/boblauer/MockDate/issues/51.

theinterned commented 3 years ago

Hey! Great news @boblauer restored the mockdate UMD build in v3.0.3 🎉 So this recipe will continue to work without any changes.

https://github.com/boblauer/MockDate/issues/51

Dmitry-Ostashev commented 3 years ago

I checked it on my side and found that script injection still doesn't work correctly:

TypeError: Cannot read property 'set' of undefined.

Could you please check if mockdate is working as a UMD module and let us know if it works without TestCafe?

theinterned commented 3 years ago

@Dmitry-Ostashev you are right! I did some digging and it turns out Typescript's UMD build does not fallback to creating a global the way WebPack and Rollup's UMD builds do 😢 microsoft/TypeScript#8436

I've updated the issue over here https://github.com/boblauer/MockDate/issues/51#issuecomment-802037305 but for now, I suppose we can stick with the v2x version of mockdate.

theinterned commented 3 years ago

@Dmitry-Ostashev I created a PR to update the README https://github.com/DevExpress/testcafe-examples/pull/50

Thank you 🙏

Dmitry-Ostashev commented 3 years ago

@theinterned Thank you for the clarification and PR. We'll review it soon.