question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Test cases should not encourage default exports

See original GitHub issue

When designing an ES6 module, the developer has two ways of exposing internal functions and values: named exports and default exports. Depending on the method used, the corresponding import statement looks a little different too. However there are various articles outlining the opinion that using default exports is an anti-pattern should be avoided.

I’m only a little way into the ECMAScript challenges, but many of the test cases I’ve come across require the default export method to be used, which I don’t think should be encouraged for many reasons.

For example take the Armstrong Numbers challenge. This challenge only requires the developer to export one function, validate. The test script consumes the function like this:

import ArmstrongNumber from './armstrong-numbers';

It then uses the function like this:

ArmstrongNumber.validate(input)

In order for the submission to pass the unit tests, the validate function has to be exported as a default export.

An alternative way of doing this is to consume the validate function like this:

import {validate} from './armstrong-numbers';

And consume it like this:

validate(input)

For the consuming module, this is a lot less typing. For the submitter the only change is that they don’t have to type ‘default’.

The Run Length Encoding exercise does this right:

import { encode, decode } from './run-length-encoding';

Not only is this less typing, but in a real-world scenario the first style will break the tree-shaking routines that are common in production code. For these and other reasons, I don’t think these exercises should be encouraging this pattern and should just consume the named function directly.

I’ve seen that there was some discussion on here about exporting classes, but I’ve not seen anything on this particular subject. I’m happy to submit refactored test cases myself. Would you be happy to accept them?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
tejasbubanecommented, Jan 24, 2019

Ok I am working on a PR to change them.

0reactions
SleeplessBytecommented, Jan 24, 2019

Closed by #599, Follow up #600

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why we have banned default exports in JavaScript
Default exports were introduced because often a module wants to export a single value or piece of functionality. In such cases, in module ......
Read more >
Default Exports in JavaScript Modules Are Terrible - Reddit
I generally don't encourage instantiating at a module level. It can cause weird unexpected side effect behavior, particularly in tests, where ...
Read more >
Jest - How to mock a ES6 default export and then Change the ...
The default export is not a mock function, you should use jest.fn() to create a mock function with a mock implementation. jest.mock('.
Read more >
Change: Prefer default export to no default export (#20) · Issues
All modules will always export named exports, meaning a future change will not affect the existing code. Names will always be explicit and...
Read more >
Avoid ES6 default exports - Rajesh Naroth - Medium
2. It is very easy to confuse default exports vs named ones while importing from the same package. Ever paused wondering if you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found