First, I thought firebase functions were broken. Then, I tried to make a simple function that returns Promise. I put that to top-level index.js.
const testPromise = param => {
console.log('return promise', param);
return new Promise((resolve, reject) => {
console.log('resolve promise');
resolve('derp');
});
};
testPromise('hede')
.then(d => {
console.log('resolved');
})
.catch(e => {
console.log('e', e);
});
AppRegistry.registerComponent(appName, () => App);
Console output:
return promise hede
resolve promise
See, there is no 'resolved' log nor error log.
I tried to change nodejs versions with nvm but no luck. I tried v12.5.0, v12.18.2, v15.6.0, v10.16.3. I tried like nvm use 12.5 && npm start
I've tried to create a new react-native project and I copied everything. Now, it works as expected. It is a solution for me but I didn't mark the question as solved because there is still a mystery, I couldn't figure why it doesn't work on the existing project.
It seems that setting inlineRequires to false inside the metro.config.js file resolves the issue.
module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
};
After making that change the output is:
return promise hede
resolve promise
resolved
Source: https://github.com/facebook/react-native/issues/31558#issuecomment-878342213
Related
For example, I created a simple package ToBeDebugged with the following functions and export:
function doSomethingFirst(myMessage) {
return new Promise((resolve, reject) => {
setTimeout(() => {
// Want to create a breakpoint in here to inspect some stuff
resolve(myMessage);
}, 2000);
})
}
function doSomethingSecond() {
console.log('Will do something second');
}
module.exports = {
doSomethingSecond,
doSomethingFirst
}
After running npm link in some other directory I install it with npm install ../path/to/ToBeDebugged, now using VSCode, I want to add a breakpoint inside setTimeout to inspect some values, but it doesn't work. What's a way to fix this?
Thanks.
I am using rollup (version 2.56.3) hook closeBundle() in a plugin I wrote to postprocess my build using spawn. When I run, rollup hangs after the postprocess. It does print "make done....", which indicates the postprocess went fine, but then sits there without returning to the shell prompt. I have to press ^c to get back to shell prompt. Here is the hook function,
function make() {
return {
closeBundle() {
let elMake = require('child_process').spawn('npm', ['run', 'make'], {
stdio: ['ignore', 'inherit', 'inherit'],
shell: true
});
return new Promise((resolve, reject) => {
elMake.on('error', reject)
elMake.on('exit', code => {
if (code === 0) {
console.log("make done....");
resolve()
} else {
const err = new Error(`make exited with code ${code}`)
reject(err)
}
})
})
}
};
}
What am I doing wrong? Any idea why is this happening?
It was happening due to MS typescript compiler bug. After some digging with google search finally found the answer. Read this thread for the details https://giters.com/rollup/rollup/issues/4213. On some platforms this was hanging up rollup.
It has been fixed with TypeScript 4.4.3
I am overwriting jsdom localstorage API like this in jest, so as to test my try catch code.
Storage.prototype.setItem = jest.fn(() => {
throw new Error("error");
});
How do reset its implementation ?
I am presently doing this. Is there a cleaner way to do this ?
const setImplementation = Storage.prototype.setItem;
Storage.prototype.setItem = jest.fn(() => {
throw new Error("error");
});
expect(() => {
localStorageHelper.setData(key, value);
}).not.toThrow();
Storage.prototype.setItem = setImplementation;
done();
Object methods should never be mocked by assigning, exactly because they cannot be restored this way. Restoring them manually is reinventing the wheel because this can be handled by Jest:
jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
throw new Error("error");
});
A mock like this one should be used with jest.restoreAllMocks() or restoreMock configuration option to be consistently restored without a risk to affect other tests.
I have this method, that starts saving an image from a stream:
saveFromStream() {
const imageName = this.getImageName();
console.log('flag of stream:',this.flag);
this.response.data.pipe(fs.createWriteStream(this.dest + imageName), { encoding: 'binary',flags:this.flag })
return new Promise((resolve, reject) => {
this.response.data.on('end', () => {
resolve()
})
this.response.data.on('error', () => {
reject()
})
})
}
The "this.flag" comes from user config object. When i pass 'wx', i see that existing files are still being overwritten. I don't understand why. As u can see, i'm passing the flag in the options object.
It works perfectly fine when i use fs.writeFile(which throws an 'EEXIST' error if the file exists), but not with fs.createWriteStream.
Any idea why?
Trying to write some tests for my hapi server. The following code is from https://github.com/hapijs/lab/issues/79 , but it's failing because done is not a function...
const Code = require('code');
const Lab = require('lab');
const lab = exports.lab = Lab.script();
lab.test('expect an error from a promise', (done) => {
return new Promise((resolve, reject) => {
try {
resolve(2);
}
catch (err) {
reject(err);
}
}).then((result) => {
console.log('5) resolved');
done(new Error('promise should be rejected and caught'));
}).catch((error) => {
console.log('5) rejected, this does not trigger');
Code.expect(error).to.exist();
done(error);
});
});
What else should I import to be able to call done?
Failed tests:
1) expect an error from a promise:
done is not a function
lab.test does not return the done callback anymore since its compatible with hapi v17.
Lab uses async/await features now and you can return promises.
See here for an example: lab docs
"lab": "^14.3.4" seems to still support done and es6 at the same time. Of course, I cannot confirm that the full set of es6 features is supported but it satisfied my needs.