Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking formatDate #248

Closed
dargmuesli opened this issue Apr 29, 2021 · 5 comments
Closed

Mocking formatDate #248

dargmuesli opened this issue Apr 29, 2021 · 5 comments

Comments

@dargmuesli
Copy link

dargmuesli commented Apr 29, 2021

Hey there, before upgrading to v2 I want to add a test that asserts nothing breaks.
My issue is that the DTSTAMP is dynamically generated, so I cannot check for a static result. I first think of mocking the function, which provides the fields value to receive a predictable result:

g += 'DTSTAMP:' + formatDate(this.calendar.timezone(), this.data.stamp) + '\r\n';

But formatDate is not exported on the library level. How can I go about this then? Could we add it as exported member or is there a better way to do it?

@sebbo2002 sebbo2002 self-assigned this Apr 30, 2021
sebbo2002 pushed a commit that referenced this issue Apr 30, 2021
# [2.1.0-develop.1](v2.0.1-develop.3...v2.1.0-develop.1) (2021-04-30)

### Features

* Export `formatDate` and some other tool functions ([6142e11](6142e11)), closes [#248](#248)
@sebbo2002
Copy link
Owner

🎉 This issue has been resolved in version 2.1.0-develop.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@dargmuesli
Copy link
Author

🎉

@dargmuesli
Copy link
Author

dargmuesli commented May 1, 2021

Well, me is very sad and frustrated. Looks like jestjs/jest#936 explains that it isn't really possible to replace transitive function calls. I've now spent way too many hours trying to work my way around it, but it doesn't really seem possible. There are babel-powered solutions like https://github.com/speedskater/babel-plugin-rewire, but I don't get why I'd need to use extensive transpilation and that typescript doesn't offer this functionality itself.

Solutions like this: https://stackoverflow.com/a/47976589/4682621
Or these: greeting.exports.js, greeting.dependency-injection.js
But as far as I see, those won't solve the issue in this case? Hmm, I don't really know. Either way, I've already spent way too many hours on this. But I'd be very glad if you'd give your opinion on this. Until then, I already want to thank you for your great support!

@dargmuesli dargmuesli mentioned this issue May 1, 2021
@dargmuesli
Copy link
Author

dargmuesli commented May 1, 2021

Oh my,

beforeAll(() => {
  jest.useFakeTimers('modern')
  jest.setSystemTime(new Date(0))
})

afterAll(() => {
  jest.useRealTimers()
})

just saved my well being. Sniff.
It mocks the environment time so I can simply check for 19700101T010000 or similar.

@github-actions github-actions bot mentioned this issue May 10, 2021
github-actions bot pushed a commit that referenced this issue May 13, 2021
# [2.1.0](v2.0.0...v2.1.0) (2021-05-13)

### Features

* Allow use of `require('ical-generator')` without `.default` ([31833a6](31833a6)), closes [#253](#253)
* Export `formatDate` and some other tool functions ([6142e11](6142e11)), closes [#248](#248)
* first commit ([dc8fa4a](dc8fa4a))
@sebbo2002
Copy link
Owner

🎉 This issue has been resolved in version 2.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants