Did you get an error?
Your code should work.
Alternatively try this:
train_dataset = datasets.MNIST(root=dirpath, train=True, transform=trans)
test_dataset = datasets.MNIST(root=dirpath, train=False, transform=trans)

dataset = ConcatDataset([train_dataset, test_dataset])

Both codes should be equivalent, so let me know, if mine works.