Executing WebClient.chat.postMessage in my environment causes the following error.
console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Headers User-Agent forbidden
at dispatchError (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:65:19)
at validCORSPreflightHeaders (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:102:5)
at Request.preflightClient.on.resp (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:380:12)
at Request.emit (events.js:182:13)
at Request.onRequestResponse (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/request/request.js:1066:10)
at ClientRequest.emit (events.js:182:13)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:546:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
at TLSSocket.socketOnData (_http_client.js:432:20)
at TLSSocket.emit (events.js:182:13) undefined
I ran the following code using Jest.
import { WebAPICallResult, WebClient } from "@slack/client";
describe("SlackNotifier", () => {
it("should send message to Slack's channel", async () => {
const client = new WebClient("My Token");
const channel = "#my-channel";
const message = "hello";
const params = {
channel: channel,
text: message
};
await client.chat
.postMessage(params)
.then((result: WebAPICallResult) => {
return Promise.resolve(result);
})
.catch((error: Error) => {
return Promise.reject(error);
});
});
});
x in one of the [ ])x in each of the [ ])@slack/client version: 4.8.0
node version: v10.2.1
OS version(s): macOS High Sierra 10.13.6
Message is sent to my channel.
The following error occurs.
console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Headers User-Agent forbidden
at dispatchError (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:65:19)
at validCORSPreflightHeaders (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:102:5)
at Request.preflightClient.on.resp (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:380:12)
at Request.emit (events.js:182:13)
at Request.onRequestResponse (/Users/kogakeita/git/aws-lambda-node-logger/node_modules/request/request.js:1066:10)
at ClientRequest.emit (events.js:182:13)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:546:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:109:17)
at TLSSocket.socketOnData (_http_client.js:432:20)
at TLSSocket.emit (events.js:182:13) undefined
I am confirming that this code works on @slack/client 4.3.1.
@keitakn are you running this code in the front-end of a web application? this package doesn't officially support browser execution, but it has worked for some people. i can try to help, but the issue might just end up being that you aren't using the right package for your use case.
the error's stack trace indicates that its being thrown in the code for js-dom. perhaps js-dom does not support setting the User-Agent header for outgoing HTTP requests. that is a capability this package needs, so is it possible for you to remove js-dom from this part of your code?
@aoberoi I am running this code from a Mac terminal.
import { WebAPICallResult, WebClient } from "@slack/client";
describe("SlackNotifier", () => {
it("should send message to Slack's channel", async () => {
const client = new WebClient("My Token");
const channel = "#my-channel";
const message = "hello";
const params = {
channel: channel,
text: message
};
await client.chat
.postMessage(params)
.then((result: WebAPICallResult) => {
return Promise.resolve(result);
})
.catch((error: Error) => {
return Promise.reject(error);
});
});
});
When this code was executed with Jest, I got an error that I reported.
@aoberoi Succeeded by setting testEnvironment to Node as in this article.
:jest.config.json
{
"collectCoverageFrom": [
"src/**/*.{ts,tsx}"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"moduleNameMapper": {
"^src/(.*)$": "<rootDir>/src/$1"
},
"roots": [
"test"
],
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(js|jsx|ts|tsx)$",
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"verbose": true,
"testEnvironment": "node"
}
Apparently it was caused by js-dom being used by default of Jest.
Thank you, this problem has been solved馃憤
I think this issue can be closed because it was solved by @keitakn
Most helpful comment
@aoberoi Succeeded by setting
testEnvironmenttoNodeas in this article.:jest.config.json { "collectCoverageFrom": [ "src/**/*.{ts,tsx}" ], "moduleFileExtensions": [ "ts", "tsx", "js", "jsx", "json", "node" ], "moduleNameMapper": { "^src/(.*)$": "<rootDir>/src/$1" }, "roots": [ "test" ], "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(js|jsx|ts|tsx)$", "transform": { "^.+\\.tsx?$": "ts-jest" }, "verbose": true, "testEnvironment": "node" }Apparently it was caused by
js-dombeing used by default of Jest.Thank you, this problem has been solved馃憤