Skip to content

Commit

Permalink
Fix #248 by adding a new option deferInitialization (#1303)
Browse files Browse the repository at this point in the history
Co-authored-by: Sarah Jiang <[email protected]>
  • Loading branch information
seratch and srajiang authored Feb 14, 2022
1 parent bbb2553 commit 446622c
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 96 deletions.
11 changes: 9 additions & 2 deletions examples/custom-properties/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const app = new App({
},
unhandledRequestTimeoutMillis: 2000, // the default is 3001
}),
// This option enables developer to call #init() in async/await style
deferInitialization: true,
});

app.use(async ({ logger, context, next }) => {
Expand All @@ -41,7 +43,12 @@ app.use(async ({ logger, context, next }) => {

(async () => {
// Start your app
await app.start(process.env.PORT || 3000);

try {
await app.init();
await app.start(process.env.PORT || 3000);
} catch (e) {
console.error(e);
process.exit(255);
}
console.log('⚡️ Bolt app is running!');
})();
4 changes: 2 additions & 2 deletions examples/custom-properties/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"name": "bolt-js-custom-properties-app",
"version": "1.0.0",
"description": "Having custom request properties in ⚡️ Bolt for JavaScript",
"main": "app.js",
"main": "http.js",
"scripts": {
"ngrok": "ngrok http 3000",
"start": "node app.js",
"start": "node http.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"license": "MIT",
Expand Down
43 changes: 43 additions & 0 deletions src/App.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,49 @@ describe('App', () => {
// Assert
assert.instanceOf(app, MockApp);
});

it('should fail in await App#init()', async () => {
// Arrange
const fakeConstructor = sinon.fake();
const overrides = mergeOverrides(withNoopAppMetadata(), {
'@slack/web-api': {
WebClient: class {
public constructor() {
fakeConstructor(...arguments); // eslint-disable-line prefer-rest-params
}

public auth = {
test: () => {
throw new Error('Failing for init() test!');
},
};
},
},
});

const MockApp = await importApp(overrides);
const app = new MockApp({
token: 'xoxb-completely-invalid-token',
signingSecret: 'invalid-one',
deferInitialization: true,
});
// Assert
assert.instanceOf(app, MockApp);
try {
// call #start() before #init()
await app.start();
assert.fail('The start() method should fail before init() call');
} catch (err: any) {
assert.equal(err.message, 'This App instance is not yet initialized. Call `await App#init()` before starting the app.');
}
try {
await app.init();
assert.fail('The init() method should fail here');
} catch (err: any) {
assert.equal(err.message, 'Failing for init() test!');
}
});

// TODO: tests for ignoreSelf option
// TODO: tests for logger and logLevel option
// TODO: tests for providing botId and botUserId options
Expand Down
Loading

0 comments on commit 446622c

Please sign in to comment.