-
Notifications
You must be signed in to change notification settings - Fork 2
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
Testing sagas #69
Comments
It's strange though; the wiki post from ARc seems to imply that importing the sagas and testing them should not pose any problem.
For clarification, refer to this comment (diegohaz/arc#101 (comment)). Instead of mocking sagas, it might just be possible to mock the |
From facebook/create-react-app#517 (comment)
I'll start looking into this possibility. |
ARc 위키에서 import하기 쉬울거라는 얘기는 없지 않나? 그리고 애초에 위키에서 말하는 테스팅은 컴포넌트들을 mock한 상태일테니 그다음에는 require.context 문제 없이 쉽게 되겠지 |
The wiki post I linked to above was about sagas, not components. Specifically, it is about unit/integration-testing sagas. It doesn't explain how the sagas could be tested if they cannot even be imported.
But yes, the wiki says nothing about "importing" the sagas, so it might just be that the tests and the sagas being tested are even in the same file. |
Installing
|
babel-plugin-transform-require-context 같은 플러그인들 보니까 |
Defining a mock // This condition actually should detect if it's an Node environment
if (typeof require.context === 'undefined') {
const fs = require('fs');
const path = require('path');
require.context = (base = '.', scanSubDirectories = false, regularExpression = /\.js$/) => {
const files = {};
function readDirectory(directory) {
fs.readdirSync(directory).forEach((file) => {
const fullPath = path.resolve(directory, file);
if (fs.statSync(fullPath).isDirectory()) {
if (scanSubDirectories) readDirectory(fullPath);
return;
}
if (!regularExpression.test(fullPath)) return;
files[fullPath] = true;
});
}
readDirectory(path.resolve(__dirname, base));
function Module(file) {
return require(file);
}
Module.keys = () => Object.keys(files);
return Module;
};
} results in the same error as above with
And this nearly rules out the solution of mocking I'm more interested in the latter, as the former seems rather ARc-specific, and thus hard to search online. |
Just to confirm, I've also tried using |
In const req = require.context('.', true, /\.\/.+\/sagas\.js$/)
// ...
const sagas = []
req.keys().forEach((key) => {
sagas.push(req(key).default)
})
export default function* () {
yield sagas.map(fork)
} Printing
The only thing we need is the list of directories of sagas files. We may be able to implement this without using |
I want to try out the solution that uses |
None of the following changed the result:
I tried adding these by adding this near the end of the config = {
...config,
// node: {
// fs: 'empty',
// },
// target: "async-node",
} |
Adding the following to the var fs = require('fs');
var nodeModules = {};
fs.readdirSync('node_modules')
.filter(function(x) {
return ['.bin'].indexOf(x) === -1;
})
.forEach(function(mod) {
nodeModules[mod] = 'commonjs ' + mod;
});
config = {
...config,
target: 'node',
externals: nodeModules,
} results in the aforementioned error with |
Trying to dynamically import config = {
...config,
externals: {
fs: 'require(\'fs\')',
},
} |
At this point, I'm starting to believe that it might be a better idea to manually provide an array of paths for sagas files for A better solution would be to study webpack and webpack configuration to figure out how |
Even hardcoding is difficult, since I don't know how I would replace the req.keys().forEach((key) => {
sagas.push(req(key).default)
}) I've decided to give up on testing sagas. I'll have to modify ARc and study webpack to even hope for a clue, and our deadline is on Monday. |
Mocking |
I'm currently trying to create tests for sagas for MainPage, but when I import any saga function into the test file:
running
npm test
results in an error:This is a problem with ARc, since it doesn't mock sagas (diegohaz/arc#131 (comment)).
I'm trying to figure out if there is a way to mock sagas for testing. As a reference, I'm following this guide to build tests for sagas.
The text was updated successfully, but these errors were encountered: