Skip to content

Commit

Permalink
Receive initial delta update from HmrServer
Browse files Browse the repository at this point in the history
Reviewed By: mjesun

Differential Revision: D10527979

fbshipit-source-id: 9098222a6cb012dcfe56d03a4fd9a66587deb508
  • Loading branch information
Alexandre Kirszenberg authored and facebook-github-bot committed Oct 29, 2018
1 parent 142659e commit f388ccd
Show file tree
Hide file tree
Showing 7 changed files with 427 additions and 231 deletions.
2 changes: 2 additions & 0 deletions packages/metro/src/HmrServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class HmrServer<TClient: Client> {
revisionId: id,
};

await this._handleFileChange(client);

const unlisten = this._bundler
.getDeltaBundler()
.listen(
Expand Down
153 changes: 107 additions & 46 deletions packages/metro/src/HmrServer/__tests__/HmrServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ describe('HmrServer', () => {
let callbacks;
let mockedGraph;

const hiModule = {
dependencies: new Map(),
inverseDependencies: new Set(),
path: '/root/hi',
getSource: () => "alert('hi');",
output: [
{
type: 'js/module',
data: {
map: [],
code: '__d(function() { alert("hi"); });',
},
},
],
};

beforeEach(() => {
mockedGraph = {
dependencies: new Map(),
Expand All @@ -48,6 +64,16 @@ describe('HmrServer', () => {
},
getRevision: getRevisionMock,
getRevisionByGraphId: getRevisionByGraphIdMock,
updateGraph: jest.fn().mockResolvedValue({
revision: {
id: 'rev0',
graph: mockedGraph,
},
delta: {
modified: new Map(),
deleted: new Set(),
},
}),
};
createModuleIdMock = path => {
return path + '-id';
Expand Down Expand Up @@ -140,43 +166,71 @@ describe('HmrServer', () => {
expect(client).toBe(null);
});

it('should return the correctly formatted HMR message after a file change', async () => {
it('should send an initial update when a client connects', async () => {
const sendMessage = jest.fn();

incrementalBundlerMock.updateGraph.mockResolvedValue({
revision: {
id: 'rev0',
graph: mockedGraph,
},
delta: {
modified: new Map([[hiModule.path, hiModule]]),
deleted: new Set(['/root/bye']),
},
});

await hmrServer.onClientConnect(
'/hot?bundleEntry=EntryPoint.js&platform=ios',
sendMessage,
);

const hiModule = {
dependencies: new Map(),
inverseDependencies: new Set(),
path: '/root/hi',
getSource: () => "alert('hi');",
output: [
{
type: 'js/module',
data: {
map: [],
code: '__d(function() { alert("hi"); });',
},
const messages = sendMessage.mock.calls.map(call => JSON.parse(call[0]));
expect(messages).toMatchObject([
{
type: 'update-start',
},
{
type: 'update',
body: {
revisionId: 'rev0',
modules: [
[
'/root/hi-id',
'__d(function() { alert("hi"); },"/root/hi-id",[],"hi",{});',
],
],
deleted: ['/root/bye-id'],
sourceURLs: ['/root/hi'],
sourceMappingURLs: [expect.anything()],
},
],
};
},
{
type: 'update-done',
},
]);
});
it('should return the correctly formatted HMR message after a file change', async () => {
const sendMessage = jest.fn();

incrementalBundlerMock.updateGraph = jest.fn().mockReturnValue(
Promise.resolve({
revision: {
id: 'revision-id',
graph: mockedGraph,
},
delta: {
modified: new Map([['/root/hi', hiModule]]),
deleted: new Set(['/root/bye']),
},
}),
await hmrServer.onClientConnect(
'/hot?bundleEntry=EntryPoint.js&platform=ios',
sendMessage,
);

sendMessage.mockReset();

incrementalBundlerMock.updateGraph.mockResolvedValue({
revision: {
id: 'rev1',
graph: mockedGraph,
},
delta: {
modified: new Map([[hiModule.path, hiModule]]),
deleted: new Set(['/root/bye']),
},
});

const promise = callbacks.get(mockedGraph)();
jest.runAllTimers();
await promise;
Expand All @@ -190,7 +244,7 @@ describe('HmrServer', () => {
{
type: 'update',
body: {
revisionId: 'revision-id',
revisionId: 'rev1',
modules: [
[
'/root/hi-id',
Expand Down Expand Up @@ -233,7 +287,9 @@ describe('HmrServer', () => {
sendMessage,
);

incrementalBundlerMock.updateGraph = jest.fn().mockImplementation(() => {
sendMessage.mockReset();

incrementalBundlerMock.updateGraph.mockImplementation(() => {
const transformError = new SyntaxError('test syntax error');
transformError.type = 'TransformError';
transformError.filename = 'EntryPoint.js';
Expand All @@ -243,24 +299,29 @@ describe('HmrServer', () => {

await callbacks.get(mockedGraph)();

expect(JSON.parse(sendMessage.mock.calls[0][0])).toEqual({
type: 'update-start',
});
const sentErrorMessage = JSON.parse(sendMessage.mock.calls[1][0]);
expect(sentErrorMessage).toMatchObject({type: 'error'});
expect(sentErrorMessage.body).toMatchObject({
type: 'TransformError',
message: 'test syntax error',
errors: [
{
description: 'test syntax error',
filename: 'EntryPoint.js',
lineNumber: 123,
const messages = sendMessage.mock.calls.map(call => JSON.parse(call[0]));

expect(messages).toMatchObject([
{
type: 'update-start',
},
{
type: 'error',
body: {
type: 'TransformError',
message: 'test syntax error',
errors: [
{
description: 'test syntax error',
filename: 'EntryPoint.js',
lineNumber: 123,
},
],
},
],
});
expect(JSON.parse(sendMessage.mock.calls[2][0])).toEqual({
type: 'update-done',
});
},
{
type: 'update-done',
},
]);
});
});
Loading

0 comments on commit f388ccd

Please sign in to comment.